rus*_*sds 4 foreign-key-relationship asp.net-mvc-3
我是MVC的新手,并坚持应该是一个非常直接的问题.我正在完成这个教程并得到了一切非常有用的工作,除了我现在想要添加一个外键'链接'(不确定它叫什么)但似乎无法让它工作.这就是我所拥有的:
表:
Inventory:
Id | SerialNumber | ManufacturerId (foreignkey to Manufactueres->id)
Manufactureres
Id (primary key) | Name
Run Code Online (Sandbox Code Playgroud)
模型(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; }
}
Run Code Online (Sandbox Code Playgroud)
查看(Create.cshtml):
...
//What i really want is a dropdown of the values in the Manufactureres table
@Html.EditorFor(model=> model.ManufacturerId)
Run Code Online (Sandbox Code Playgroud)
当使用关系数据库时,这必然是一个常见的问题,将会有许多外键关系被使用/显示,但由于某种原因,我无法在stackoverflow上找到直接对应于如此简单的东西的教程或问题.非常感谢任何指导或方向!谢谢,
我希望我能正确理解你的问题.似乎您想要添加新的库存项目,然后您需要下拉列表中的所有制造商的列表.我打算做这个假设,如果我不在赛道上,请告诉我:)
首先去创建一个视图模型.此视图模型将绑定到yout视图.永远不要将域对象绑定到您的视图.
public class InventoryItemViewModel
{
public int SerialNumber { get; set; }
public int ManufacturerId { get; set; }
public IEnumerable<Manufacturer> Manufacturers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
您的域对象:
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; }
}
Run Code Online (Sandbox Code Playgroud)
您的控制器可能如下所示:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的视图中,您可能会有以下内容:
@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>
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助 :)
| 归档时间: |
|
| 查看次数: |
13543 次 |
| 最近记录: |