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

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) 的外键引用。

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

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

而且我必须将 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;
    }

数据库架构

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

谢谢。

4

2 回答 2

12

向字符串数组类型的 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并分配给SiteBrandsSiteConfig 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;
}

现在在你的视图中,它是对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" />
}

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

在此处输入图像描述

于 2012-08-13T14:31:42.073 回答
0

您可以拥有一个包含站点和品牌模型的“ViewModel”。然后您可以将您的视图绑定到该模型。这将允许您将视图的任何部分绑定到任何底层模型的任何部分。

public class siteViewModel{
    public SiteConfig x;
    public Brand b; //Fill this with all the available brands
}

当然,您可以包含您的视图可能需要的任何其他信息(也减少了 ViewBag 的需要)。

于 2012-08-13T14:06:09.913 回答