C# asp.net MVC4从另一个视图中的视图访问变量(ActionLink)
我的林业管理应用程序存在以下问题: Index.cshtml:C# asp.net MVC4从另一个视图中的视图访问变量(ActionLink),c#,asp.net-mvc-4,view,actionlink,C#,Asp.net Mvc 4,View,Actionlink,我的林业管理应用程序存在以下问题: Index.cshtml: @model IEnumerable<ForestryManagement.Models.Forestry> @foreach (var forestry in Model) { <li>@Html.ActionLink(forestry.Name, "Index12", new {tree = forestry.ForestryID}) } @model IEnum
@model IEnumerable<ForestryManagement.Models.Forestry>
@foreach (var forestry in Model)
{
<li>@Html.ActionLink(forestry.Name, "Index12", new {tree = forestry.ForestryID})
}
@model IEnumerable<ForestryManagement.Models.Tree>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.treeName)
</td>
</tr>
@Html.ActionLink("Create new Tree", "Create", new { id = **??????**}) //need ForestyID from Index.cshtml here
@model IEnumerable
@foreach(模型中的var)
{
@ActionLink(forestry.Name,“Index12”,新的{tree=forestry.ForestryID})
}
当您单击一个林业(ActionLink)时,您将被重定向到Index12,其中列出了这些林业中的树木
Index12.cshtml:
@model IEnumerable<ForestryManagement.Models.Forestry>
@foreach (var forestry in Model)
{
<li>@Html.ActionLink(forestry.Name, "Index12", new {tree = forestry.ForestryID})
}
@model IEnumerable<ForestryManagement.Models.Tree>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.treeName)
</td>
</tr>
@Html.ActionLink("Create new Tree", "Create", new { id = **??????**}) //need ForestyID from Index.cshtml here
@model IEnumerable
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.treeName)
@ActionLink(“创建新树”,“创建”,新建{id=**???????**})//此处需要Index.cshtml中的ForestyID
现在,当我要在此林中创建另一棵树时,我需要ActioLink的Index12.cshtml页面上的林中ID创建树列表的视图模型,如下所示:
public class TreeListingViewModel
{
public int ForestryId { get; set; }
public IEnumerable<TreeViewModel> Trees { get; set; }
}
您需要使用视图模型,而不是将数据实体传递给视图,并将每个视图的ID放入模型中。