我有一个星期实体和一个日实体,每个实体都是表和控制器。因为它在我的应用程序中,你必须分别创建星期和天。在周标签中,如果您点击"详情"在一周内,您可以在表格中查看日期和数据。
我的问题是,如何创建一个按钮,将您带到DayController
创建页面,以便我可以从周标签中查看日期。
以下是本周详细信息的HTML,我是新手,我认为这是可以帮助您理解的代码:
@model Utilities.Models.Week
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Week</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.WeekID)
</dt>
<dd>
@Html.DisplayFor(model => model.WeekID)
</dd>
<dt>
@Html.DisplayNameFor(model => model.WeekNo)
</dt>
<dd>
@Html.DisplayFor(model => model.WeekNo)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Days)
</dt>
<dd>
<table class="table">
<tr>
<th>
WeekID
</th>
<th>
DayNo
</th>
<th>
Day of the Week
</th>
<th>
Data 1
</th>
<th>
More data
</th>
<th></th>
</tr>
@foreach (var item in Model.Days)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Week.WeekID)
</td>
<td>
@Html.DisplayFor(modelItem => item.DayNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.DayofWeek)
</td>
<td>
@Html.DisplayFor(modelItem => item.Data1)
</td>
<td>
@Html.DisplayFor(modelItem => item.More Data)
</td>
</tr>
}
</table>
</dd>
</dl>
</div>
<p>
/*
| My attempt at going to the create day page below but it doesn't work. Am I referencing it wrong?
| Basically, I want to click "Create Day" and go to the create page for Day.
*/
@Html.ActionLink("Create Day", "Create", new { id = Model.Days.DayID}) |
@Html.ActionLink("Edit", "Edit", new { id = Model.WeekID }) |
@Html.ActionLink("Back to List", "Index")
</p>
我可以包含可能需要帮助解答的任何其他代码。
Days
是Week模型中的导航属性。
当我运行上面的代码时,我得到:
CS1061: 'System.Collections.Generic.ICollection<Utilities.Models.Day>' does not contain a definition for 'DayID' and no extension method 'DayID' accepting a first argument of type 'System.Collections.Generic.ICollection<Utilities.Models.Day>' could be found (are you missing a using directive or an assembly reference?)
但是Day确实有DayID。
答案 0 :(得分:0)
由于该错误,我认为您的Model.Days
是Day
个对象的集合,因此可以找到名为DayID
的属性。要完成您的魔杖,您需要在该集合中指定所需对象的索引。请尝试以Model.Days[0].DayID
为例告诉我这是否有效。
代码:
@model Utilities.Models.Week
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Week</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.WeekID)
</dt>
<dd>
@Html.DisplayFor(model => model.WeekID)
</dd>
<dt>
@Html.DisplayNameFor(model => model.WeekNo)
</dt>
<dd>
@Html.DisplayFor(model => model.WeekNo)
</dd>
<dt>
@{
var count = 0;
foreach(var day in Model.Days)
{
@Html.DisplayNameFor(model => day.DayofWeek)
if(count!= Model.Days - 1)
{ @(", ") }
count++;
}
</dt>
<dd>
<table class="table">
<tr>
<th>
WeekID
</th>
<th>
DayNo
</th>
<th>
Day of the Week
</th>
<th>
Data 1
</th>
<th>
More data
</th>
<th></th>
</tr>
@foreach (var item in Model.Days)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Week.WeekID)
</td>
<td>
@Html.DisplayFor(modelItem => item.DayNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.DayofWeek)
</td>
<td>
@Html.DisplayFor(modelItem => item.Data1)
</td>
<td>
@Html.DisplayFor(modelItem => item.MoreData)
</td>
<td>
@Html.ActionLink("Create Day", "Create", new { id = item.DayID})
</td>
</tr>
}
</table>
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.WeekID }) |
@Html.ActionLink("Back to List", "Index")
</p>
更新:
如果您想创建新的一天,只需更改代码:
<p>
@Html.ActionLink("Create Day", "Create", "Day")
@Html.ActionLink("Edit", "Edit", new { id = Model.WeekID }) |
@Html.ActionLink("Back to List", "Index")
</p>