Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC 5-模型中包含HTML内容的奇怪行为_C#_Html_.net_Asp.net Mvc_Exception - Fatal编程技术网

C# ASP.NET MVC 5-模型中包含HTML内容的奇怪行为

C# ASP.NET MVC 5-模型中包含HTML内容的奇怪行为,c#,html,.net,asp.net-mvc,exception,C#,Html,.net,Asp.net Mvc,Exception,我正在开发一个基于ASP.NET MVC 5的功能,用于管理以html格式呈现的消息模板。在viewmodel中使用html标记会导致一些问题 这些消息模板通过所见即所得编辑器进行编辑 以下是控制器的基本示例: 部分观点 @使用(Html.BeginForm(“模板”,“黑板”,FormMethod.Post,新的{@class=“form horizontal”,role=“form”})) { 消息 @Html.TextAreaFor(m=>m.Message,新{rows=“20”,sty

我正在开发一个基于ASP.NET MVC 5的功能,用于管理以html格式呈现的消息模板。在viewmodel中使用html标记会导致一些问题

这些消息模板通过所见即所得编辑器进行编辑

以下是控制器的基本示例: 部分观点
@使用(Html.BeginForm(“模板”,“黑板”,FormMethod.Post,新的{@class=“form horizontal”,role=“form”}))
{
消息
@Html.TextAreaFor(m=>m.Message,新{rows=“20”,style=“resize:none;width:400px;”,placeholder=Html.DisplayNameFor(m=>m.Message),@class=“表单控制输入lg textarea编辑器”})
}

当我将html标记发布到控制器操作时,一切正常。为了实现这一点,我必须用AllowHtml属性修饰包含标记的model属性

但是:如果ModelState无效,例如TemplateName为null,那么我仍然会得到
HttpRequestValidationException
表示:

“潜在危险的请求。从中检测到表单值。” “客户”

我无法用那个基本的例子重现这种行为,但它发生在我更复杂的web应用程序中。在一些站点上,我发现了这样的信息:如果有任何内容涉及控制器或视图的Request属性,就会抛出异常。我试图解决这个问题,但似乎没用。另外,我不知道哪些组件实际上正在访问请求或包含对该请求的引用


如果ModelState是有效的,我怎么会看不到这个异常呢。当ModelState无效时,如何抛出
HttpRequestValidationException

可能重复@KennethK。不是重复的sind我正在使用AllowHtml属性,但仍然得到一个奇怪的行为。如果你从TemplateName中删除[Required],它会给你同样的错误吗?@BrianP不,我不会得到同样的错误。不知何故,它当时似乎起了作用。但是这个属性需要标记为必需的。@Tom我打赌它与路线选择有关。它正在寻找不需要TemplateName的路由。如果这是您使用ViewModel的唯一位置,我将删除必需的属性,然后手动处理该验证。这也是确保客户端执行它的良好实践。
public class BlackboardController : Controller
{
    public ActionResult Template()
    {
        return View(new RichTextEditorViewModel()
        {
            Message = "<h1>I'm a headline</h1><p>I'm a regular text...</p>"
        });
    }

    [HttpPost]
    public ActionResult Template(RichTextEditorViewModel model)
    {
        if (!ModelState.IsValid)
            return View(model);

        return RedirectToAction("Template");
    }
}
public class RichTextEditorViewModel
{
    [Required]
    [Display(Name = "Template name")]
    public string TemplateName { get; set; }

    [AllowHtml]
    [Display(Name = "Message")]
    public string Message { get; set; }
}
@using (Html.BeginForm("Template", "Blackboard", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    <div class="form-group">
        <label class="col-md-4 control-label">Message </label>
        <div class="col-md-8">
            <div class="input-group">
                @Html.TextAreaFor(m => m.Message, new { rows = "20", style = "resize:none;width:400px;", placeholder = Html.DisplayNameFor(m => m.Message), @class = "form-control input-lg textarea-editor" })
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save template" class="btn btn-default" />
        </div>
    </div>
}