gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:22:47 25 4
gpt4 key购买 nike

我的模型是

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com