[英]How to store HTML code in ASP.NET MVC2 Model
我有一个带有字符串属性的注释模型,如下所示:
[Column]
public string Text { get; set; }
注释文本中可以包含所有HTML标记(我知道这很不好,但是我必须这样做)。 但是,当我更新对象时,MVC 2会转义所有HTML标记。
更新方法是:
[HttpPost]
public ActionResult Edit(int ID=0)
{
Comment comment= ID == 0
? new Comment ()
: commentRepository.Comments.First(x => x.ID == ID);
TryUpdateModel(comment);
if (ModelState.IsValid)
{
commentRepository.Save(comment);
return RedirectToAction("View/" + comment.ID);
}
else
{
return View(comment);
}
}
如何在不逃脱的情况下更新评论文本?
PS另外我对列类型也有问题:当我将SQL Server Express中的列Text从varchar切换到text时 ,更新模型失败:
数据类型text和nvarchar在等于运算符中不兼容。
异常详细信息:System.Data.SqlClient.SqlException:数据类型text和nvarchar在等于运算符中不兼容。
您需要在[HttpPost]过滤器下的控制器上添加以下过滤器;
[ValidateInput(false)]
您要做的是关闭对特定输入字段的验证。 为此,您可以将此过滤器添加到操作中:
[ValidateInput(false)]
或者,如果您想特定,只在一个字段上将其关闭(也许可以在多个字段上使用,尽管我尚未进行过测试),则可以执行以下操作:
[ValidateInput(true, Exclude = "YourFieldName")]
这将排除您的字段,但仍会检查所有其他字段。
ADDON:可以通过(AFAIK)两种方式添加过滤器。 这里有两个样本:
[HttpPost, ValidateInput(true, Exclude = "YourFieldName")]
public ActionResult SomeAction(...) { ... }
[HttpPost]
[ValidateInput(true, Exclude = "YourFieldName")]
publiv ActionResult SomeAction(...) { ... }
<configuration> <system.web> <httpRuntime requestValidationMode="2.0"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.