如何解决在表单提交的表单字段中发送HTML标记
| 我试图向我的控制器操作提交表单,表单字段之一包含HTML标签,例如<p>
<ul>
我已经添加了
<httpRuntime requestValidationMode=\"2.0\"/>
和
<pages
validateRequest=\"false\"
</pages>
在我的web.config
中
我已经添加了
controller5ѭ我的控制器动作
但是当我在ѭ6til字段包含HTML标记时提交表单时仍然出现以下错误:
A potentially dangerous Request.Form value was detected from the client (desc=\"<ul><li>Comments: Th...\").
我应该/应该做什么,在此先感谢...
我正在使用VS 2010,MVC和实体框架
编辑
这是视图边码
<% using (Html.BeginForm(\"UpdateProductVariant\",\"home\",new { Id = Model.productVariant.ProductVariantId },FormMethod.Post))
{%>
<table>
<tr>
<td>Name:</td>
<td><%: Html.TextArea(\"ProductVariantName\",Model.product.Name)%></td>
</tr>
<tr>
<td>Description:</td>
<td><%= Html.TextArea(\"desc\",Model.productVariant.Description)%></td>
</tr>
控制器端
[ValidateInput(false)]
public ActionResult UpdateProductVariant(int id,FormCollection collection,bool pub,bool del,decimal hprice)
{
解决方法
以下应该工作:
模型:
public class MyViewModel
{
public string Description { get; set; }
}
控制器:
public class HomeController: Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
Description = \"<p>Hello</p>\"
};
return View(model);
}
[ValidateInput(false)]
[HttpPost]
public ActionResult UpdateProductVariant(MyViewModel model)
{
...
}
}
视图:
<% using (Html.BeginForm(\"UpdateProductVariant\",\"home\",new { Id = Model.productVariant.ProductVariantId },FormMethod.Post)) { %>
<%= Html.TextAreaFor(x => x.Description) %>
<input type=\"submit\" value=\"OK\" />
<% } %>
如果您正在运行ASP.NET 4.0,则只需将“ 2”添加到web.config中。另外,您无需添加<pages validateRequest=\"false\" />
。
如果这不起作用,则可能是您未使用[ValidateInput(false)]
属性修饰正确的控制器动作。这应该是您将表单过帐到的操作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。