asp.net-mvc-3 – 使用MVC3中的模型绑定ListBox
我的模特是
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.所以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; } 有人可以建议如何绑定列表框并获取所选项目. 谢谢. 解决方法
将新属性添加到类型为字符串数组的SiteConfig ViewModel.当用户发布此表单时,我们将使用此选项从列表框中获取Selected项.
public class SiteConfig { //Other properties here public string[] SelectedBrands { get; set; } // new proeprty public IEnumerable<SiteBrand> SiteBrands { get; set; } } 在您的GET操作方法中,获取SiteBrands列表并分配给SiteConfig ViewModel对象的SiteBrands属性 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 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" /> } 现在,当用户发布此表单时,您将获得ViewModel的SelectedBrands属性中的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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 更好的方式来获取页面名称
- 解决“这个操作需要IIS集成管道模式”在ASP.net MVC2
- 我不能在ASP.NET MVC中做的事情
- asp.net – 永远不想在highcharts上隐藏工具提示
- IIS或ASP.NET是否创建了包含请求URL的ETW事件?
- iis-7 – ASP.NET应用程序吃内存. Application / Session对
- 关闭一个子目录的ASP.Net WebForms身份验证
- asp.net-mvc – Web API返回csv文件
- asp.net-mvc – ASP.NET MVC – 通过GET提交时,让Html.Begi
- asp.net-mvc-5 – 使用属性路由的路由优先级
- asp.net-mvc – 如何使用带有IEnumerable的Html.
- 我不应该将哪些ASP.NET MVC项目文件添加到Subver
- 在ASP.NET MVC中启用了对Javascript的测试
- asp.net – 如何将@Model传递给Angular ng-init
- asp.net-mvc – 通过集合迭代并在Razor中打印索引
- 使用C#实现网站用户登录
- asp.net-mvc – 存在多个获取操作时的MVC API路由
- asp.net-mvc-3 – MVC3未终止字符串常量语法错误
- asp.net – 什么项目层应该屏幕DTO的生活?
- asp.net-mvc – ASP.Net MVC和WebAPI加密