简体   繁体   English

asp.net MVC4从另一个视图中的一个视图访问变量(ActionLink)

[英]asp.net MVC4 Access to a variable from a view in another view (ActionLink)

i have the following problem with my Forestry Management Applpication: 我的林业管理应用存在以下问题:

Index.cshtml: Index.cshtml:

@model IEnumerable<ForestryManagement.Models.Forestry>

 @foreach (var forestry in Model)
    {
        <li>@Html.ActionLink(forestry.Name, "Index12", new {tree = forestry.ForestryID})


    }

When you click on one Forestry (ActionLink), you will be redirected to the Index12, where the trees from these Forestry are listed 当您单击一个林业(ActionLink)时,您将被重定向到Index12,其中列出了这些林业中的树木

Index12.cshtml: Index12.cshtml:

@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

Now, when i will create another tree in this forestry, i need the ID from the forestry on my Index12.cshtml page for the ActioLink 现在,当我要在该林业中创建另一棵树时,我需要在ActioLink的Index12.cshtml页面上获得林业的ID

Create a view model for tree listings, like so: 为树列表创建视图模型,如下所示:

public class TreeListingViewModel
{
    public int ForestryId { get; set; }

    public IEnumerable<TreeViewModel> Trees { get; set; }
}

Note: Prefer creating a TreeViewModel instead of just using your Tree data entity to reduce the dependency between your view and your data model (and also increase security as people can quite easily inject properties into your database if you expose your data entities directly to your view). 注意:最好创建一个TreeViewModel而不是仅使用Tree数据实体来减少视图与数据模型之间的依赖关系(并且还可以提高安全性,因为如果将数据实体直接暴露给视图,人们可以很容易地将属性注入数据库中) )。

Pass that to your view instead: 将其传递给您的视图:

@model TreeListingViewModel

...

@Html.ActionLink("Create new Tree", "Create", new { id = Model.ForestryId })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM