在 ASP.NET MVC 3 中,您可以使用属性装饰需要接受此输入的模型的[AllowHtml]
属性。这样,您就不会被迫禁用整个控制器操作的输入验证,这是以前通过使用[ValidateInput]
属性装饰它来完成的。所以在你的模型上
public class MathematicsViewModel
{
public int Id { get; set; }
public string Name { get; set; }
[AllowHtml]
public string MathematicFormula { get; set; }
}
然后让你的控制器动作:
[HttpPost]
public ActionResult(MathematicsViewModel model)
{
// model.MathematicFormula will now accept input like $x<y>z$
...
}
在您的视图中,您可以有一个名为的文本框MathematicFormula
,用户可以在其中键入这些字符,并且您不会遇到异常。
另外不要忘记在 web.config 中设置以下内容,否则此属性在 .NET 4.0 中无效(这是 ASP.NET MVC 3 使用的):
<httpRuntime requestValidationMode="2.0"/>