对于大型嵌套复选框列表,ASP.NET MVC Form POST非常慢

时间:2018-01-05 15:16:56

标签: c# asp.net asp.net-mvc performance data-binding

我的ASP.NET MVC Web应用程序出现问题,我的ViewModel在发布表单时需要约30秒才能点击我的控制器。我猜这与默认的Model Binder有关。

[HttpPost]
public ActionResult Edit(ByActivityEditViewModel viewModel)
{
    if (ModelState.IsValid) // Takes ~30 seconds before even hitting this
    {

我的ViewGroup个父母和User个孩子的一系列嵌套复选框。多个User下可列出相同的Groups。我使用EditorFor生成ViewModel的复选框。

查看编辑模板致电: @Html.EditorFor(model => model.Groups)

编辑模板:

@model MyProject.Models.Group
@Html.HiddenFor(model => model.Guid)
@Html.HiddenFor(model => model.Name)
@Html.CheckBoxFor(model => model.IsAllowed, new { @class = Model.Guid.ToString(), @style = "margin-right:5px; cursor:pointer;" }) @Html.LabelFor(model => model.IsAllowed, Model.Name, new { @class = "build-checkbox-label", @style = "font-weight:normal; margin-top:-2px;" })

@if (Model.Users.Any())
{
    <ul style="list-style:none;">
        @for (int i = 0; i < Model.Users.Count; i++)
        {
            <li>
                @Html.HiddenFor(model => Model.Users[i].Guid)
                @Html.HiddenFor(model => Model.Users[i].Name)
                @Html.CheckBoxFor(model => Model.Users[i].IsAllowed, new { @class = Model.Users[i].Guid.ToString(), @style = "margin-right:5px; cursor:pointer;" }) @Html.LabelFor(model => Model.Users[i].IsAllowed, Model.Users[i].Name, new { @class = "build-checkbox-label", @style = "font-weight:normal; margin-top:-2px;" })
            </li>
        }
    </ul>
}

视图模型:

public class ByActivityEditViewModel
{
    public int ActivityId { get; set; }
    public string Path { get; set; }
    public IList<Group> Groups { get; set; } = new List<Group>();
}

public class Group
{
    public Guid? Guid { get; set; }
    public string Name { get; set; }
    public string DistinguishedName { get; set; }
    public string SamAccountName { get; set; }
    public string DisplayName { get; set; }
    public bool IsAllowed { get; set; }
    public List<User> Users { get; set; } = new List<User>();
}

public class User
{
    public Guid? Guid { get; set; }
    public string Name { get; set; }
    public string DistinguishedName { get; set; }
    public string SamAccountName { get; set; }
    public string DisplayName { get; set; }
    public bool IsAllowed { get; set; }
    public bool IsUserChecked { get; set; }
}

的ModelState:

enter image description here

模型状态包含Guids,Names和IsAllowed值。我认为处理这个问题很慢。

最终成为:

  • 64组
  • 853用户

用户可以是多个群组的一部分。

我尝试过:

  1. 将字符串用于Guid并稍后解析。
  2. 删除我正在使用的BeginCollectionItem包。
  3. 对我的显示器大喊大叫。
  4. 任何建议,信息或解决方法都将受到赞赏。

2 个答案:

答案 0 :(得分:1)

Jquery / js函数以前已经为我做了几次这样的事情,当处理大量的控件时,我已经试图弄明白了。现在我知道先去哪里看。

答案 1 :(得分:0)

因此,除非您阅读注释,否则不清楚帖子的解决方法是什么。因此,答案是添加@ {Html.EnableClientValidation(false); },然后@using(Html.BeginForm())

这将关闭默认的客户端验证,并使您将表单过帐到服务器的速度更快。当然,如果您需要客户端验证,那么这不是解决方案。在我的情况下,我的表单有200行带有复选框,并且不需要客户端验证。因此,该解决方案对我来说非常有用。