asp.net-mvc-3 – 在mvc的下拉列表中使用外键
发布时间:2020-12-16 07:28:24 所属栏目:asp.Net 来源:网络整理
导读:我是MVC的新手,并坚持应该是一个非常直接的问题.我正在努力完成 this tutorial并且一切都工作得很好,除了我现在想要添加一个外键’链接'(不确定它叫什么)但似乎无法让它工作.这就是我所拥有的: 表: Inventory: Id | SerialNumber | ManufacturerId (foreig
我是MVC的新手,并坚持应该是一个非常直接的问题.我正在努力完成
this tutorial并且一切都工作得很好,除了我现在想要添加一个外键’链接'(不确定它叫什么)但似乎无法让它工作.这就是我所拥有的:
表: Inventory: Id | SerialNumber | ManufacturerId (foreignkey to Manufactueres->id) Manufactureres Id (primary key) | Name 模型(InventoryItem.cs): public class InventoryItem { public int Id {get; set; } public int SerialNumber{ get; set; } //this starts the trouble,I actually want to interact with the Manufactureres table -> Name column public int ManufacturerId { get; set; } } 查看(Create.cshtml): ... //What i really want is a dropdown of the values in the Manufactureres table @Html.EditorFor(model=> model.ManufacturerId) 当使用关系数据库时,这必然是一个常见的问题,将会有许多外键关系被使用/显示,但由于某种原因,我无法在stackoverflow上找到直接对应于如此简单的东西的教程或问题.非常感谢任何指导或方向! 解决方法
我希望我能正确理解你的问题.似乎您想要添加新的库存项目,然后您需要下拉列表中的所有制造商的列表.我打算做这个假设,如果我不在赛道上,请告诉我:)
首先去创建一个视图模型.此视图模型将绑定到yout视图.永远不要将域对象绑定到您的视图. public class InventoryItemViewModel { public int SerialNumber { get; set; } public int ManufacturerId { get; set; } public IEnumerable<Manufacturer> Manufacturers { get; set; } } 您的域对象: public class InventoryItem { public int Id { get; set; } public int SerialNumber{ get; set; } public int ManufacturerId { get; set; } } public class Manufacturer { public int Id { get; set; } public string Name { get; set; } } 您的控制器可能如下所示: public class InventoryItemController : Controller { private readonly IManufacturerRepository manufacturerRepository; private readonly IInventoryItemRepository inventoryItemRepository; public InventoryItem(IManufacturerRepository manufacturerRepository,IManufacturerRepository manufacturerRepository) { // Check that manufacturerRepository and inventoryItem are not null this.manufacturerRepository = manufacturerRepository; this.inventoryItemRepository = inventoryItemRepository; } public ActionResult Create() { InventoryItemViewModel viewModel = new InventoryItemViewModel { Manufacturers = manufacturerRepository.GetAll() }; return View(viewModel); } [HttpPost] public ActionResult Create(InventoryItemViewModel viewModel) { // Check that viewModel is not null if (!ModelState.IsValid) { Manufacturers = manufacturerRepository.GetAll() return View(viewModel); } // All validation is cool // Use a mapping tool like AutoMapper // to map between view model and domain model InventoryItem inventoryItem = Mapper.Map<InventoryItem>(viewModel); inventoryItemRepository.Insert(inventoryItem); // Return to which ever view you need to display return View("List"); } } 然后在您的视图中,您可能会有以下内容: @model MyProject.DomainModel.ViewModels.InventoryItems.InventoryItemViewModel <table> <tr> <td class="edit-label">Serial Number <span class="required">**</span></td> <td>@Html.TextBoxFor(x => x.SerialNumber,new { maxlength = "10" }) @Html.ValidationMessageFor(x => x.SerialNumber) </td> </tr> <tr> <td class="edit-label">Manufacturer <span class="required">**</span></td> <td> @Html.DropDownListFor( x => x.ManufacturerId,new SelectList(Model.Manufacturers,"Id","Name",Model.ManufacturerId),"-- Select --" ) @Html.ValidationMessageFor(x => x.ManufacturerId) </td> </tr> </table> 我希望这有帮助 :) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- asp.net-mvc – 使用类似MvcContrib Grid的东西在
- asp.net-mvc – ASP.NET MVC 4路由 – controlle
- asp.net – 天蓝色的动态子域
- asp.net-mvc – ReadOnly(true)是否与Html.Edito
- ASP.NET自定义控件:何时调用LoadPostData()?
- asp.net – 如何使用Ninject注入依赖项,其中实例
- asp.net – 无法识别配置文件Web.config
- Asp.net 通用万级数据分页代码[修正下载地址]
- asp.net-web-api – 仅允许某些域访问web api
- ASP.NET Core Web读取appsettings.json中的配置
热点阅读