gpt4 book ai didi

asp.net-mvc-3 - 在 mvc 的下拉列表中使用外键

转载 作者:行者123 更新时间:2023-12-02 03:00:43 24 4
gpt4 key购买 nike

我是 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 上找不到直接对应于如此简单的内容的教程或问题。非常感谢任何指导或方向!谢谢,

最佳答案

我希望我正确理解你的问题。似乎当您想要添加新的库存项目时,您需要在下拉列表中列出所有制造商的列表。我将致力于这个假设,如果我偏离了轨道,请告诉我:)

首先创建一个 View 模型。您将将此 View 模型绑定(bind)到您的 View 。切勿将域对象绑定(bind)到您的 View 。

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

您的 Controller 可能如下所示:

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-3 - 在 mvc 的下拉列表中使用外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12519280/

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