Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.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# Jquery selected Multiselect MVC 5未在编辑表单上填充选定值_C#_Jquery_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# Jquery selected Multiselect MVC 5未在编辑表单上填充选定值

C# Jquery selected Multiselect MVC 5未在编辑表单上填充选定值,c#,jquery,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Jquery,Asp.net,Asp.net Mvc,Asp.net Mvc 4,以下是我的视图模型: public class AccEditViewModel { [Required] public int Id { get; set; } [Required] [Display(Name = "Branch")] public List<long> Branches { get; set; } [Required] [Display(Name = "User Type")] public s

以下是我的视图模型:

public class AccEditViewModel
{
    [Required]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Branch")]
    public List<long> Branches { get; set; }

    [Required]
    [Display(Name = "User Type")]
    public string UserType { get; set; }

    [Required]
    [Display(Name = "IsActive")]
    public bool IsActive { get; set; }
}
公共类AccEditViewModel
{
[必需]
公共int Id{get;set;}
[必需]
[显示(Name=“Branch”)]
公共列表分支{get;set;}
[必需]
[显示(Name=“用户类型”)]
公共字符串用户类型{get;set;}
[必需]
[显示(Name=“IsActive”)]
公共bool IsActive{get;set;}
}
以下是我在控制器中的操作方法:

public ActionResult Edit(int? id)
{
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        ApplicationUser user = UserManager.FindById((int)id);

        if (user == null)
        {
            return HttpNotFound();
        }

        ApplicationDbContext db = new ApplicationDbContext();

        AccEditViewModel model = new AccEditViewModel()
        {                
            Id = user.Id,
            IsActive = user.IsActive,
            UserType = user.UserType,
        };

        model.Branches = new List<long>();

        foreach (Branch branch in user.Branches)
        {
            model.Branches.Add(branch.BranchID);
        }

        ViewBag.Branches = db.Branches.Where(x => x.IsActive == true).AsEnumerable();
        ViewBag.UserTypes = new SelectList(UserTypes.GetUserTypes(), "Value", "Type", user.UserType);

        return View(model);
}
public ActionResult编辑(int?id)
{
if(id==null)
{
返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ApplicationUser user=UserManager.FindById((int)id);
if(user==null)
{
返回HttpNotFound();
}
ApplicationDbContext db=新的ApplicationDbContext();
AccEditViewModel=新的AccEditViewModel()
{                
Id=user.Id,
IsActive=user.IsActive,
UserType=user.UserType,
};
model.branchs=新列表();
foreach(user.branchs中的分支)
{
model.branchs.Add(branch.BranchID);
}
ViewBag.branchs=db.branchs.Where(x=>x.IsActive==true).AsEnumerable();
ViewBag.UserTypes=新的选择列表(UserTypes.GetUserTypes(),“值”,“类型”,user.UserType);
返回视图(模型);
}
以下是我的观点:

<div class="form-group">
    @Html.LabelFor(m => m.Branches, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.ListBox("Branches", new MultiSelectList(ViewBag.Branches, "BranchID", "Name", Model.Branches.AsEnumerable()), new { @class = "form-control branches-select" })
        @Html.ValidationMessageFor(m => m.Branches)
    </div>
</div>
<script src="@Url.Content("~/Scripts/chosen.jquery.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/chosen.css")" rel="stylesheet" type="text/css" /> 
<script> $(".branches-select").chosen(); </script> 

@LabelFor(m=>m.branchs,新的{@class=“col-md-2控制标签”})
@Html.ListBox(“branchs”,新的MultiSelectList(ViewBag.branchs,“branchhid”,“Name”,Model.branchs.AsEnumerable()),新的{@class=“form control branchs select”})
@Html.ValidationMessageFor(m=>m.branchs)
$(“.branchs select”).select();
当我使用模型中的
分支
属性时,它在没有填充选定值的情况下显示,这是一个问题,当我使用另一个未在ViewModel中清除的属性时,工作正常,但不使用我在POST方法中需要的ViewModel属性

请告诉我这个代码出了什么问题


非常感谢

尝试为您的ViewBag集合使用与模型属性不同的名称

控制器

ViewBag.BranchList = db.Branches.Where(x => x.IsActive == true).AsEnumerable();
看法

@Html.ListBox("Branches", new MultiSelectList(ViewBag.BranchList,"BranchID","Name" ,Model.Branches.AsEnumerable()), new { @class = "form-control branches-select" })