TextboxFor 元素和 CheckboxFor 元素为空且未发送到 POST

TextboxFor element and CheckboxFor element are null and not sent to POST

我有一个奇怪的情况,我们有这个视图模型:

public class AddCommunicationViewModel
    {
        public Guid Id { get; set; }
        public Guid ParentCommunicationGroupId { get; set; }
        public Guid ParentCommunicationGroupCampaignId { get; set; }
        public List<SelectListItem> CommunicationTypes { get; set; } 
        public CommunicationType SelectedCommunicationTypeId { get; set; }

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

        [AllowHtml]
        public string Content { get; set; }
        public string Reference { get; set; }
        public bool AttachDemand { get; set; }
        public int CommunicationSortingIndex { get; set; }
        public string CommunicationGroupName { get; set; }


        public AddCommunicationViewModel()
        {
            CommunicationTypes = new List<SelectListItem>();
        }
    }

现在,我有一个非常简单的表单(使用 Html.BeginForm),它 post 发送给控制器:

@using (Html.BeginForm("UpdateSavedCommunication", "Communication", FormMethod.Post, new { id = "saveCommunication" }))
{
    @Html.HiddenFor(c => c.ParentCommunicationGroupCampaignId)
    @Html.HiddenFor(c => c.ParentCommunicationGroupId)
    @Html.HiddenFor(c => c.Id)


    <div class="form-group">
        @Html.LabelFor(c => c.SelectedCommunicationTypeId)
        @Html.DropDownListFor(c => c.SelectedCommunicationTypeId, Model.CommunicationTypes, new { @class = "form-control" })
    </div>

    <div class="div_subject form-group email">
        @Html.LabelFor(c => c.Subject)
        @Html.TextBoxFor(c => c.Subject, new { @class = "form-control" })
    </div>

    <div class="div_content form-group mb-3">
        @Html.LabelFor(c => c.Content)
        <div class="card-body">
            <form class="form-horizontal form-bordered">
                <div class="form-group row">
                    <div class="col-sm-12">
                        <textarea name="Content" id="summernote" class="summernote" data-plugin-summernote data-plugin-options='{ "height": 280, "codemirror": { "theme": "ambiance" } }'>
                            @Html.Raw(Model.Content)
                        </textarea>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="div_reference form-group email">
        @Html.LabelFor(c => c.Reference)
        @Html.TextBoxFor(c => c.Reference, new { @class = "form-control" })
    </div>


    <div class="div_attachdemand form-group mb-3">
        <div>
            @Html.LabelFor(c => c.AttachDemand)
            @Html.CheckBoxFor(c => c.AttachDemand)
        </div>
    </div>


    <input type="submit" class="btn btn-success" value="Gem" onclick="document.getElementById('saveCommunication').submit();" />
}

我的 "Content" 变量有点奇怪的原因是因为我使用的是 Summernote 编辑器。当我单击提交按钮时,所有 post 都正常,没有任何 JavaScript 错误。

然而,奇怪的事情来了。

我的变量:ReferenceAttachDemand 未 post 发送到服务器。查看 Fiddler,我可以看到这是 post:

ParentCommunicationGroupCampaignId=00000000-0000-0000-0000-000000000000&ParentCommunicationGroupId=00000000-0000-0000-0000-000000000000&Id=2953bc1b-49e5-4f65-8159-1c53de5c82e8&SelectedCommunicationTypeId=2&Subject=&Content=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%3Cp%3E%3Cspan+style%3D%22color%3A+rgb%280%2C+0%2C+0%29%3B%22%3EHej%C2%A0*%7CNAME%7C*.%C2%A0%3C%2Fspan%3E%3C%2Fp%3E%3Cp%3E%3Cspan+style%3D%22color%3A+rgb%280%2C+0%2C+0%29%3B%22%3E*%7CCREDITORNAME%7C*+har+bedt+os+inddrive+din+g%C3%A6ld+hos+dem.+Der+er+tale+om+en+g%C3%A6ld+p%C3%A5%3C%2Fspan%3E%3Cspan+style%3D%22color%3A+rgb%280%2C+0%2C+0%29%3B%22%3E%C2%A0%3C%2Fspan%3E%3Cspan+style%3D%22color%3A+rgb%280%2C+0%2C+0%29%3B%22%3E*%7CDEMANDLINK%7C*+kr%2C+som+du+bedes+betale.%3Cbr%3EVi+har+vedh%C3%A6ftet+kravet+i+dette+brev+p%C3%A5+e-mail.%3C%2Fspan%3E%3C%2Fp%3E%3Cp%3E%3Cspan+style%3D%22color%3A+rgb%280%2C+0%2C+0%29%3B%22%3EDet+er+vigtigt+at+du+betales+denne+g%C3%A6ld+hurtigst+muligt%2C+for+at+undg%C3%A5+at+der+p%C3%A5l%C3%B8ber+ekstra+renter+samt+en+registrering+hos+RKI+og+som+videre+konsekvens+sendt+til+fogedretten.%3C%2Fspan%3E%3C%2Fp%3E%3Cp%3E%3Cspan+style%3D%22color%3A+rgb%280%2C+0%2C+0%29%3B%22%3ESE+DIN+SAG+OG+BETAL%3A+*%7CDEMANDLINK%7C*%C2%A0%3C%2Fspan%3E%3C%2Fp%3E%3Cp%3E%3Cspan+style%3D%22color%3A+rgb%280%2C+0%2C+0%29%3B%22%3E%3Cbr%3E%3C%2Fspan%3E%3C%2Fp%3E%3Cp%3E%3Cspan+style%3D%22color%3A+rgb%280%2C+0%2C+0%29%3B%22%3EVh%3Cbr%3E%3C%2Fspan%3E%3C%2Fp%3E%0D%0A++++++++++++++++++++++++%0D%0A++++++++++++++++++++++++%0D%0A++++++++++++++++++++++++%0D%0A++++++++++++++++++++++++%0D%0A++++++++++++++++++++++++&files=

或在人类视图中:

所以我的挑战是:我的 ReferenceAttachDemand 在哪里?

现在,如果我将这两个移动到内容编辑器上方,它们就可以工作了。所以似乎 Content 正在中断,因为它发送了一些非常酷的 HTML.

那么知道如何 "make" 这项工作吗?我想我需要以某种聪明的方式以某种方式逃避输入?我在内容属性上添加了 [AllowHtml],因为它将包含 HTML,但不止于此?

您的视图包含无效 html 且不受支持的嵌套表单(根据浏览器和版本的不同,您可能会看到不同的结果)。

删除文本区域周围的内部 <form class="form-horizontal form-bordered"> 以确保所有来自控件的序列化并在请求中发送。