gpt4 book ai didi

asp.net-mvc - 换行问题 - C# .NET MVC

转载 作者:行者123 更新时间:2023-12-02 06:22:58 35 4
gpt4 key购买 nike

今天是个好日子!但... :)我有以下问题:我有一个 Controller 可以更新 Mysql 数据库中的 type_text 字段。用户在 texarea 中键入文本,单击“更新”,然后,哦,神奇的是,文本被发布到数据库中。但是没有休息......

在我的 Controller 中:

[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;
}

这让我疯狂了 2 天...

有人可以帮助我吗?非常感谢!

已更新

  • 我使用 jeditable 执行内联编辑。帖子字符串示例:value=Some+text%0ASome2+text2

最佳答案

我会将文本按原样 存储在数据库中而不转换 \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");
}

然后我会编写一个自定义 HTML 帮助程序来格式化 View 中的值(如有必要)以显示这些评论。

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));
}

然后在 View 中:

@Html.FormatComment(Model.Comment)

关于asp.net-mvc - 换行问题 - C# .NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5949575/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com