Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对于大型嵌套复选框列表,ASP.NET MVC表单发布速度非常慢_C#_Asp.net_Asp.net Mvc_Performance_Data Binding - Fatal编程技术网

C# 对于大型嵌套复选框列表,ASP.NET MVC表单发布速度非常慢

C# 对于大型嵌套复选框列表,ASP.NET MVC表单发布速度非常慢,c#,asp.net,asp.net-mvc,performance,data-binding,C#,Asp.net,Asp.net Mvc,Performance,Data Binding,我的ASP.NET MVC web应用程序有问题,在发布表单时,我的ViewModel需要约30秒才能击中我的控制器。我猜这与默认的模型绑定器有关,因为这样 [HttpPost] public ActionResult Edit(ByActivityEditViewModel viewModel) { if (ModelState.IsValid) // Takes ~30 seconds before even hitting this { 我的视图是组父项和用户子项的一系列

我的ASP.NET MVC web应用程序有问题,在发布表单时,我的
ViewModel
需要约30秒才能击中我的控制器。我猜这与默认的模型绑定器有关,因为这样

[HttpPost]
public ActionResult Edit(ByActivityEditViewModel viewModel)
{
    if (ModelState.IsValid) // Takes ~30 seconds before even hitting this
    {
我的
视图
父项和
用户
子项的一系列嵌套复选框。相同的
用户
可以列在多个
组下
。我正在使用
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; }
}
@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())
{
    @对于(int i=0;i @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;“}) }
}
视图模型:

@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; }
}
公共类ByActivityEditViewModel
{
public int ActivityId{get;set;}
公共字符串路径{get;set;}
公共IList组{get;set;}=new List();
}
公共课组
{
公共Guid?Guid{get;set;}
公共字符串名称{get;set;}
公共字符串区分名称{get;set;}
公共字符串SamAccountName{get;set;}
公共字符串DisplayName{get;set;}
允许公共布尔值{get;set;}
公共列表用户{get;set;}=new List();
}
公共类用户
{
公共Guid?Guid{get;set;}
公共字符串名称{get;set;}
公共字符串区分名称{get;set;}
公共字符串SamAccountName{get;set;}
公共字符串DisplayName{get;set;}
允许公共布尔值{get;set;}
公共bool IsUserChecked{get;set;}
}
ModelState:

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

模型状态包含GUID、名称和IsAllowed值。我认为这是缓慢的部分

结果是:

@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; }
}
  • 64组
  • 853个用户
用户可以是多个组的一部分

我试过了:

@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; }
}
  • 使用字符串作为Guid,并在以后对其进行分析
  • 正在删除我正在使用的BeginCollectionItem包
  • 对着我的显示器大喊大叫

  • 任何建议、信息或解决方法都将不胜感激。

    因此,除非您阅读评论,否则从帖子中不清楚修复方法是什么。因此,答案是在@using(Html.BeginForm())之前添加@Html{Html.EnableClientValidation(false);}


    这将关闭默认的客户端验证,并使您更快地向服务器发送表单。当然,如果您需要客户端验证,那么这不是解决方案。在我的例子中,我的表单有200行带有复选框,我不需要客户端验证。所以这个解决方案对我来说非常有效。

    您是否使用任何js/JQUR库来装饰或验证?例如,你应该试试Chrome开发者/调试工具,看看会发生什么。尽管如此,浏览器需要处理很多复选框。@Niko这是jquery验证,我不需要复选框。非常感谢。我在模型活页夹上完全有隧道式的视觉。如果你把这个作为答案,我很乐意给你赏金。我相信它将来会帮助别人。我使用
    @{Html.EnableClientValidation(false);}
    在视图中关闭它。