绑定列表框在MVC3模型 [英] Binding ListBox with a model in MVC3

查看:118
本文介绍了绑定列表框在MVC3模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型

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记录可以包含一个或多个品牌。所以,品牌是保存到所谓的SiteBrand另一个表。

I am following Data Base first approach. Each SiteConfig record can contain one or more Brand. So Brand is saving to another table called SiteBrand.

SiteBrand包含forign键引用都SiteConfig(上IdSiteConfig)和品牌(BrandId)。

SiteBrand contains the forign key reference to both SiteConfig(on IdSiteConfig) and Brand(BrandId).

当我创建一个SiteConfig我要显示所有可用的品牌如列表框,用户可以选择一个或多个记录(也可能不会选择任何品牌)。

When I am creating a SiteConfig I want to display all the available Brand as list box where user can select one or many record(may not select any brand).

但是,当我结合我的看法与我怎么能我的列表框绑定到的品牌列表,在视图发布我怎样才能得到所选择的品牌型号。

But when I bind my view with the model how can I bind my list box to the list of brands and when view is posted how can I get the selected brands.

和我有到SiteConfig对象保存到数据库中选定的项目。这是我的数据库图。

And I have to save the SiteConfig object to database with the selected Items. And this is my DB diagram.

这是我的DAL它保存到数据库中。

This is my DAL which saves to 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;
    }

有人可以advide如何绑定列表框,并获得选择的项目。

Can somebody advide how to bind the list box and get the selected items.

感谢。

推荐答案

添加一个新的属性到你的类型的SiteConfig视图模型字符串数组。我们将使用这个从列表框当用户发布这种形式获得所选择的项目。

Add a new Property to your SiteConfig ViewModel of type string array. We will use this to get the Selected item from the Listbox when user posts this form.

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

在您的GET操作方法,获得 SiteBrands 的列表,并分配给SiteConfig视图模型的 SiteBrands 属性对象

In your GET action method, Get a list of SiteBrands and assign to the SiteBrands property of the SiteConfig ViewModel object

public ActionResult CreateSiteConfig()
{
    var vm = new SiteConfig();
    vm.SiteBrands = GetSiteBrands();
    return View(vm);
}

有关演示目的,我只是很难codeD的方法。当你实现这一点,你可能会得到数据从数据访问层。

For demo purposes, I just hard coded the method. When you implement this, you may get the Data From your Data Access layer.

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

在您看来,这是强类型为 SiteConfig 视图模型,

Now in your View, which is strongly typed to SiteConfig ViewModel,

@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 属性选定的项目值

Now when user posts this form, you will get the Selected Items value in the SelectedBrands property of the 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);
}

这篇关于绑定列表框在MVC3模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆