"InvalidDataException: Form value count limit 1024 exceeded." 当 post 在 asp.net 核心中从视图到控制器的值超过 1000 个时,模型为空

"InvalidDataException: Form value count limit 1024 exceeded." model null when post more then 1000 values from view to controller in asp.net core

我正在尝试根据角色向 selected 候选人发送批量电子邮件。当我 selected 有限的选项然后模型正确通过时,但是一旦我 select 来自 multiselect 下拉列表的 1000 个选项和来自模型的 post 通过 null.

非常奇怪,有什么原因吗?

电子邮件模板视图模型

public class EmailTemplateViewModel
{
    public EmailTemplateViewModel()
    {
        LstEmailTemplate = new List<EmailTemplateModel>();
        
        SendEmailModel = new SendEmailModel();
    }
    public List<EmailTemplateModel> LstEmailTemplate { get; set; }
   
    public Pager Pager { get; set; }
    public SendEmailModel SendEmailModel { get; set; }
}

查看

    @model Practise.Models.EmailTemplateViewModel
@using (Html.BeginForm("SentBulkEMail", "EmailTemplate", FormMethod.Post, new { @class = "form-horizontal" }))
                {
                    @Html.HiddenFor(x => x.SendEmailModel.EmailTemplateId, new { id = "hdnEmailTemplateId" })
                    <div class="form-group">
                        <label class="col-lg-2 control-label">User Type</label>
                        <div class="col-lg-10">
                            @Html.DropDownListFor(x => x.SendEmailModel.Roles, new SelectList(Roles, "Value", "Text"), new { id = "Roles", @class = "form-control select2", multiple = "multiple", @required = "required" })
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-2 control-label">Recipients</label>
                        <div class="col-lg-10">
                            @Html.DropDownListFor(x => x.SendEmailModel.Recipient, new SelectList(ViewBag.Recipient as IEnumerable<SelectListItem>), "", new { id = "Recipients", multiple = "multiple", @required = "required", @class = "form-control" })
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-2 control-label">Subject</label>
                        <div class="col-lg-10">
                            @Html.TextBoxFor(x => x.SendEmailModel.Subject, new { id = "subject", @class = "form-control", @required = "required" })
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-2 control-label">Message</label>
                        <div class="col-lg-10">
                            @Html.TextAreaFor(x => x.SendEmailModel.Description, new { id = "hdndescription", @class = "form-control", @required = "required" })
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-10">
                            <button class="btn btn-primary" type="submit">Send</button>
                            <button class="btn btn-danger" type="button" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                }

控制器

[HttpPost]
        public async Task<IActionResult> SentBulkEMail(EmailTemplateViewModel emailTemplateViewModel)
        {
            CustomResponseModel response = new CustomResponseModel();
            response = await _emailTemplateService.SentBulkEmail(Request);
            if (response.IsSuccess)
            {
                TempData[Constant.Success] = response.Message;
            }
            else
            {
                TempData[Constant.Error] = response.Message;
            }
            return RedirectToAction("List");
        }

你可以从下图中得到更好的想法:

第一个 select multi select 只有 4 个选项

控制器当select 4个选项

第 2 select 超过 1000

控制器当我select超过1000个选项

终于找到答案了

默认请求值限制为 1024 .

为此,我们需要使用 RequestFormLimits 属性设置请求限制,如下所示:

[HttpPost]
[RequestFormLimits(ValueCountLimit = Int32.MaxValue)]
public async Task<IActionResult> SentBulkEMail(SendEmailModel emailModel)
{
}

添加此属性后 ValueCountLimit 允许请求值高达 Int32.MaxValue 或者您可以根据您的要求进行设置。