我定义了一个模型:
public class ExhibitionItemModel
{
public IEnumerable<tblItem> Items { get; set; }
public tblExhibition Exhibition { get; set; }
public IEnumerable<tblExhibitionItem> ExhibitionItems { get; set; }
}
tblItem
包含有关特定项目的信息,例如描述、代码等。
tblExhibitionItem
包含来自的 id tblItem
、展览 id 告诉系统特定项目属于哪个展览,以及该项目/展览组合的价格。
在我的控制器中,我正在填充 ExhibitionItemModel:
ExhibitionItemModel exhibitionItemModel = new ExhibitionItemModel();
exhibitionItemModel.Exhibition = db.tblExhibitions.Find(id);
exhibitionItemModel.ExhibitionItems = (from objExhibitionItems in db.tblExhibitionItems
where objExhibitionItems.ExhibitionID == id
select objExhibitionItems).AsEnumerable<tblExhibitionItem>();
exhibitionItemModel.Items = (from objItem in db.tblItems
select objItem).OrderBy(item => item.Code).AsEnumerable<tblItem>();
return View(exhibitionItemModel);
我的观点是,我列出了所有项目,我希望有一个复选框,根据项目是否在tblExhibitionItem
. 然后是一个文本框,用于输入商品的价格。
到目前为止,我有:
@foreach (var item in Model.Items)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Code)</td>
<td>@Html.DisplayFor(modelItem => item.Description)</td>
<td>@Html.CheckBoxFor(modelItem => item.AdditionalItem.Value, new { @disabled = "disabled" })</td>
<td>@Html.CheckBox("chkIsSelected")</td>
<td>@Html.Editor("txtPrice")</td>
</tr>
}
由于我是 MVC 的新手,所以我不知道如何建立复选框和价格文本框之间的链接,以显示项目与项目的链接。换句话说,当我将更改提交到数据库时,我将使用项目的 ID 和价格填充展览项目,具体取决于是否选中了复选框(“chkIsSelected”)。
此外,如果数据库中已经有展览项目,我需要更改什么来填充复选框和价格?