Search code examples
c#asp.net-coreasp.net-core-3.1bootstrap-multiselect

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


I'm trying to sent bulk email to selected candidates based on roles. When I'm selected limited options then model pass properly, but once I select more then 1000 option from multiselect dropdown and post from the model passed null.

Very Strang, Is there any reason for that?

Email Template View model

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; }
}

View

    @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>
                }

Controller

[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");
        }

You can get better idea from below image:

1st I select only 4 option from multi select enter image description here

Controller when select 4 options enter image description here

2nd select more then 1000

enter image description here

Controller when I select more than 1000 options enter image description here


Solution

  • Finally I found an answer.

    The default request value is limited up to 1024 .

    For that we need to set the request limit using RequestFormLimits attribute like follows:

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

    After adding this attribute with ValueCountLimit allow request value up to Int32.MaxValue or you can set as per your requirement.