【问题标题】:asp.net mvc dropdownlist bindingasp.net mvc 下拉列表绑定
【发布时间】:2016-12-15 16:49:27
【问题描述】:

下面是我的模型。我在 mvc 的表中创建下拉列表,但出现此错误

public class UserRegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    public IEnumerable<Role> Roles { get; set; } 
    public SelectList RoleSelectList { get; set; } 
    public Role Role { get; set; } 

    private List<Role> GetRoles() 
    { 
        List<Role> roles = new List<Role>(); 

        roles.Add(new Role() { Value = "c", RoleCode = "Corporate" }); 
        roles.Add(new Role() { Value = "d", RoleCode = "Divisional" }); 
        roles.Add(new Role() { Value = "r", RoleCode = "Regional" }); 
        roles.Add(new Role() { Value = "f", RoleCode = "Facility" }); 
        roles.Add(new Role() { Value = "s", RoleCode = "Resource" }); 
        return roles; 
    }

    public UserRegisterModel() 
    { 
        this.Roles = GetRoles(); 
        this.RoleSelectList = new SelectList(Roles, "Value", "RoleCode"); 
    } 


    public IEnumerable<UserAccinfo> UserAccounts { get; set; } 

}
public class UserAccinfo
{
    public string accountno { get; set; }
    public IEnumerable<Brand> Brands { get; set; }
    public SelectList BrandSelectList { get; set; }
    public Brand Brand { get; set; }

    private List<Brand> GetBrands() 
    {
        List<Brand> brands = new List<Brand>();

        brands.Add(new Brand() { BrandValue = "c", BrandCode = "Corporate" });
        brands.Add(new Brand() { BrandValue = "d", BrandCode = "Divisional" });
        brands.Add(new Brand() { BrandValue = "r", BrandCode = "Regional" });
        brands.Add(new Brand() { BrandValue = "f", BrandCode = "Facility" });
        brands.Add(new Brand() { BrandValue = "s", BrandCode = "Resource" });
        return brands; 
    }

    public UserAccinfo() 
    {
        this.Brands = GetBrands();
        this.BrandSelectList = new SelectList(Brands, "BrandValue", "BrandCode"); 
    } 
}
public class Brand
{
    public string BrandCode { get; set; }
    public string BrandValue { get; set; }
} 
public class Role 
{ 
    public string RoleCode { get; set; } 
    public string Value { get; set; } 
} 

如何在 mvc 视图中绑定第二个下拉列表?

【问题讨论】:

  • 你能添加你得到的错误吗?
  • 这是错误消息:方法 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System. Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable, string, object)' 无法从用法中推断出来。尝试明确指定类型参数。
  • 我在视图中这样做: @foreach (var cat in Model.UserAccounts) { }
    @Html.DropDownListFor(cat.Brand.BrandValue, cat.BrandSelectList , "--select--", null) @cat.accountno

标签: asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

在控制器中:

public ActionResult Index()
{
        UserRegisterModel UserRegisterModel = new UserRegisterModel();
        UserAccinfo UserAccinfo = new UserAccinfo();

        ViewBag.Brands = UserAccinfo.BrandSelectList;
        ViewBag.Roles = UserRegisterModel.RoleSelectList;
        return View();
}

和Index.cshtml

@Html.DropDownList("Brands");
@Html.DropDownList("Roles");

如果您想在选定品牌时更改角色 你可以这样做。

在Controller中添加一个JsonResult:

public JsonResult LoadRoles(string v)
    {
        UserRegisterModel UserRegisterModel = new UserRegisterModel();
        List<KeyValuePair<string, string>> items = new List<KeyValuePair<string, string>>();
        if (!string.IsNullOrWhiteSpace(v))
        {
            var roles = UserRegisterModel.RoleSelectList.Where(x => x.Value == v);
            if (roles.Count() > 0)
            {
                foreach (var r in roles)
                {
                    items.Add(new KeyValuePair<string, string>(r.Value,r.Text));
                }
            }
        }
        return Json(items);
    }

索引.cshtml

<script>

    $(function () {
        //When Brands change
        $("#Brands").change(function () {
            $.ajax({
                //Call postback
                url: '@Url.Action("LoadRoles", "Home")',
                data: { v: $(this).val() },
                type: 'post',
                cache: false,
                dataType: 'json',
                success: function (data) {
                    if (data.length > 0) {
                        $('#Roles').empty();
                        //add result to downlist
                        $.each(data, function (i, item) {
                            $('#Roles').append($('<option></option>').val(item.Key).text(item.Value));
                        });
                    }
                }
            });
        });
    });
</script>

【讨论】:

    【解决方案2】:
     public ActionResult  AllUsers() {
       List<Users> users = userRep.GetUsers();
      var listUsers = (from u in users.AsEnumerable()
                           select new SelectListItem
                          {
                             Text = u.UserName,
                             Value = u.UserId.ToString(),
                             Selected = (u.UserId==6)
                          }).AsEnumerable();
        // ViewBag.ListItems = listUsers; 
         ViewData["tempEmpList"]=listUsers
        //ViewBag.SelectedItem = 2;
          return View(); }
    

    正在查看

    @Html.DropDownList("SelectedEmployee", new SelectList((IEnumerable) ViewData["tempEmpList"], "Id", "Name"))**

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多