Summernote 和 MVC c# 中的表单提交

Summernote and form submission in MVC c#

我正在为文本框使用 summernote 插件: http://summernote.org/#/getting-started#basic-api

这是我使用 summmernote 的表格:

<div class="modal-body" style="max-height: 600px">
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <fieldset class="form-horizontal">
            <div id="textForLabelLanguage"></div>
            <button type="submit" class="btn btn-primary">Save changes</button>
            @Html.ActionLink("Cancel", "Index", null, new { @class = "btn " })
        </fieldset>
    }
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $('#textForLabelLanguage').summernote();
    });
</script>

现在,在我的控制器中,这是我的代码:

public ActionResult Create(UserInfo newInfo , [Bind(Prefix = "textForLabelLanguage")] string textForLabelLanguage)
{
    //logic here
}

现在的问题是 textForLabelLanguage 参数总是空的。

发生这种情况是因为我必须在提交表单时将 $('#textForLabelLanguage').code(); 传递给 MVC,但我不知道该怎么做!

如何解决我的问题?

我找到了解决问题的方法。这就是我让控制器获取正确信息的方式:

<div class="modal-body" style="max-height: 600px">
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <fieldset class="form-horizontal">
            <textarea name="textForLabelLanguage" id="textForLabelLanguage" />
            <button type="submit" class="btn btn-primary">Save changes</button>
            @Html.ActionLink("Cancel", "Index", null, new { @class = "btn " })
        </fieldset>
    }
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $('#textForLabelLanguage').summernote();
    });
</script>

基本上,如果我使用带有名称的文本区域而不是输入或其他任何东西,它就可以工作!

但是,请注意,即使此解决方案有效,我也会在控制器中收到一条错误消息:

从客户端检测到具有潜在危险的 Request.Form 值

发生这种情况是因为我允许 HTML。但这是另一个问题的问题!

与之前发布的内容类似,您可以使用 HTML 助手

@HTML.TextAreaFor( m=> m.text, new {@id = "textFor*Model*"})

而不是<textarea>

请使用[AllowHTML]

MSDN 上有一篇很好的文章 Request Validation in ASP.NET

"To disable request validation for a specific property, mark the property definition with the AllowHtml attribute:"

[AllowHtml]
public string Prop1 { get;  set; }