asp.net-mvc-3 - 在 MVC3 中将 ListBox 与模型绑定(bind)

标签 asp.net-mvc-3 c#-4.0 asp.net-mvc-4 asp.net-mvc-areas ef-database-first

我的模型是

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


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

我正在遵循数据库第一方法。每个 SiteConfig 记录可以包含一个或多个品牌。所以 Brand 正在保存到另一个名为 SiteBrand 的表中。

SiteBrand 包含对 SiteConfig(on IdSiteConfig) 和 Brand(BrandId) 的外键引用。

当我创建站点配置时,我想将所有可用的品牌显示为列表框,用户可以在其中选择一条或多条记录(可能不选择任何品牌)。

但是,当我将 View 与模型绑定(bind)时,如何将列表框绑定(bind)到品牌列表以及发布 View 时如何获取所选品牌。

而且我必须将 SiteConfig 对象与所​​选项目一起保存到数据库中。这是我的数据库图。

这是我的 DAL,它保存到 db。
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;
    }

DB Schema

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

谢谢。

最佳答案

向字符串数组类型的 SiteConfig ViewModel 添加一个新属性。我们将使用它从 Listbox 中获取 Selected 项当用户发布此表单时。

public class SiteConfig
{
  //Other properties here
  public string[] SelectedBrands { get; set; }  // new proeprty
  public IEnumerable<SiteBrand> SiteBrands { get; set; }
}

在您的 GET 操作方法中,获取 SiteBrands 的列表并分配给 SiteBrands SiteConfig ViewModel 对象的属性
public ActionResult CreateSiteConfig()
{
    var vm = new SiteConfig();
    vm.SiteBrands = GetSiteBrands();
    return View(vm);
}

出于演示目的,我只是对该方法进行了硬编码。当您实现这一点时,您可能会从数据访问层获取数据。
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;
}

现在在您的 View 中,它的强类型为 SiteConfig View 模型,
@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" />
}

现在,当用户发布此表单时,您将在 SelectedBrands 中获得 Selected Items 值。 ViewModel 的属性
[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);
}

enter image description here

关于asp.net-mvc-3 - 在 MVC3 中将 ListBox 与模型绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11935106/

相关文章:

javascript - CheckBoxFor 在 ASP.NET MVC 的 javascript 函数中返回以前的值

c# - (扩展图标栏 - chrome 浏览器)我们可以通过 selenium web 驱动程序自动执行此开/关操作吗

javascript - ajax post之后mvc url太长

asp.net - 如何防止用户代理: Eureka/1 to return source code

asp.net-mvc-3 - 如何在 MVC3 中重写 URL

c# - 我可以将 C# 变量分配给 javascript 函数的 return 语句吗?

asp.net-mvc-3 - 如何在 VIEW MVC3 Razor 中显示 Web 表单页面 .aspx

c# - 基于服务的系统中 session (跟踪)ID 的设计模式?

c# - Arg<object>.Is.Equal 与匿名对象

c# - MVC4中两个变量的RequiredIf条件验证