Evg*_*kiy 2 asp.net-mvc entity-framework line-breaks
今天天气真好!但是...... :)我有以下问题:我有一个控制器更新Mysql DB中的type_text字段.用户在texarea中键入文本,单击"更新",然后将文本发布到数据库.但没有休息......
在控制器中我有:
[Authorize]
[HttpPost]
public string EditComment(FormCollection formValues)
{
var Commenter = User.Identity.Name;
Int64 id = Convert.ToInt64(Request.Form["id"]);
string formValue = Request.Form["value"];
formValue = formValue.Replace("\r\n", "<br/>").Replace("\r", "<br/>");
comments updateCommnets = db.comments.SingleOrDefault(d => d.id == id && d.commenterName == Commenter);
updateCommnets.comment = formValue;
db.SaveChanges();
return formValue;
}
Run Code Online (Sandbox Code Playgroud)
它让我疯狂了2天......
有人可以帮帮我吗?非常感谢!
更新
我会存储文本为是在数据库中无需转换\r\n到<br/>:
[Authorize]
[HttpPost]
public ActionResult EditComment(string value, long id)
{
var commenter = User.Identity.Name;
var updateCommnets = db.comments.SingleOrDefault(d => d.id == id && d.commenterName == commenter);
updateCommnets.comment = value;
db.SaveChanges();
return Content(value, "text/plain");
}
Run Code Online (Sandbox Code Playgroud)
然后,我会编写一个自定义HTML帮助器,以便在必要时格式化视图中的值以显示这些注释.
public static MvcHtmlString FormatComment(this HtmlHelper html, string comment)
{
if (string.IsNullOrEmpty(comment))
{
return MvcHtmlString.Empty;
}
var lines = comment
.Split(
new string[] { Environment.NewLine },
StringSplitOptions.None
)
.Select(x => HttpUtility.HtmlEncode(x))
.ToArray();
return MvcHtmlString.Create(string.Join("<br/>", lines));
}
Run Code Online (Sandbox Code Playgroud)
然后在视图中:
@Html.FormatComment(Model.Comment)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3179 次 |
| 最近记录: |