代码之家  ›  专栏  ›  技术社区  ›  Lars Holdgaard

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

  •  0
  • Lars Holdgaard  · 技术社区  · 6 年前

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

    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 )向控制器发布:

    @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编辑器。当我单击submit按钮时,所有帖子都很好,没有任何JavaScript错误。

    然而,现在奇怪的事情来了。

    我的变量: Reference AttachDemand 未发布到服务器。看着Fiddler,我可以看到这是帖子:

    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=
    

    或者从人类的角度来看:

    enter image description here

    所以我的挑战是:我的 参考 附件要求 ?

    现在,如果我将这两个移动到内容编辑器上方,它们就会起作用。看来 Content 正在中断,因为它发送了一些非常酷的HTML。

    那么,你知道如何“实现”这一目标吗?我想我需要以某种聪明的方式逃避输入?我已添加 [AllowHtml] 因为它将包含HTML,但不仅仅如此?

    1 回复  |  直到 6 年前
        1
  •  2
  •   user3559349 user3559349    6 年前

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

    拆下内部 <form class="form-horizontal form-bordered"> 围绕textarea以确保所有来自控件都序列化并在请求中发送。