将ListBox与MVC3中的模型绑定

Nar*_*esh 4 asp.net-mvc-areas c#-4.0 asp.net-mvc-3 ef-database-first asp.net-mvc-4

我的模特是

public class SiteConfig
{
    public SiteConfig()
    {

    }

    public int IdSiteConfig { get; set; }
    public string Name { get; set; }
    public byte[] SiteLogo { get; set; }
    public string Brands { get; set; }
    public string LinkColour { get; set; }

    public IEnumerable<SiteBrand> SiteBrands { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

public class SiteBrand
{
    public int Id { get; set; }
    public int SiteId { get; set; }
    public int BrandId { get; set; }

    public Brand Brand { get; set; }
    public SiteConfig SiteConfig { get; set; }
}

public class Brand
{
    public int BrandId { get; set; }
    public string Name { get; set; }

    public IEnumerable<SiteBrand> SiteBrands { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我正在关注数据库的第一种方法.每个SiteConfig记录可以包含一个或多个Brand.所以Brand正在保存到另一个名为SiteBrand的表中.

SiteBrand包含对SiteConfig(在IdSiteConfig上)和Brand(BrandId)的forign键引用.

当我创建SiteConfig时,我想显示所有可用的品牌列表框,用户可以在其中选择一个或多个记录(可能不会选择任何品牌).

但是当我将我的视图与模型绑定时,如何将我的列表框绑定到品牌列表,当发布视图时,我如何才能获得所选品牌.

我必须使用所选项目将SiteConfig对象保存到数据库.这是我的数据库图.

这是我保存到db的DAL.

public SiteConfig Add(SiteConfig item)
    {
        var siteConfig = new Entities.SiteConfig
            {
                Name = item.Name,
                LinkColour = item.LinkColour,
                SiteBrands = (from config in item.SiteBrands
                              select new SiteBrand {BrandId = config.BrandId, SiteId = config.SiteId}).
                    ToList()
            };
        _dbContext.SiteConfigs.Add(siteConfig);
        _dbContext.SaveChanges();
        return item;
    }
Run Code Online (Sandbox Code Playgroud)

DB Schema

有人可以建议如何绑定列表框并获取所选项目.

谢谢.

Shy*_*yju 12

将新属性添加到类型为字符串数组的SiteConfig ViewModel.我们将使用它来从Listbox用户发布此表单时获取Selected项.

public class SiteConfig
{
  //Other properties here
  public string[] SelectedBrands { get; set; }  // new proeprty
  public IEnumerable<SiteBrand> SiteBrands { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在GET操作方法中,获取SiteConfig ViewModel对象SiteBrandsSiteBrands属性列表并分配给该属性

public ActionResult CreateSiteConfig()
{
    var vm = new SiteConfig();
    vm.SiteBrands = GetSiteBrands();
    return View(vm);
}
Run Code Online (Sandbox Code Playgroud)

出于演示目的,我只是对该方法进行了硬编码.实现此功能后,您可以从数据访问层获取数据.

public IList<SiteBrand> GetSiteBrands()
{
    List<SiteBrand> brands = new List<SiteBrand>();
    brands.Add(new SiteBrand { Brand = new Brand { BrandId = 3, Name = "Nike" } });
    brands.Add(new SiteBrand { Brand = new Brand { BrandId = 4, Name = "Reebok" } });
    brands.Add(new SiteBrand { Brand = new Brand { BrandId = 5, Name = "Addidas" } });
    brands.Add(new SiteBrand { Brand = new Brand { BrandId = 6, Name = "LG" } });
    return brands;
}
Run Code Online (Sandbox Code Playgroud)

现在在View中,它是SiteConfigViewModel的强类型,

@model SiteConfig
<h2>Create Site Config</h2>
@using (Html.BeginForm())
{
  @Html.ListBoxFor(s => s.SelectedBrands, 
                new SelectList(Model.SiteBrands, "Brand.BrandId", "Brand.Name"))
  <input type="submit" value="Create" />
}
Run Code Online (Sandbox Code Playgroud)

现在,当用户发布此表单时,您将SelectedBrands在ViewModel 的属性中获取Selected Items值

[HttpPost]
public ActionResult CreateSiteConfig(SiteConfig model)
{
    if (ModelState.IsValid)
    {
        string[] items = model.SelectedBrands;
        //check items now
        //do your further things and follow PRG pattern as needed
    }
    model.SiteBrands = GetBrands();
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述