asp.net-mvc-4 - 尝试访问显示模板中父 View 模型的属性

标签 asp.net-mvc-4 parent-child display-templates partialviews

我正在尝试构建一个使用部分 View 和显示/编辑器模板和 Kendo ui 的 asp.net mvc 4 应用程序。
我有一个特定页面的 View 模型:

public class Plant {
    public Guid PlantId{ get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ICollection<Leaf> Leafs{ get; set; }
    public ICollection<Flower> Flowers{ get; set; }
    public ICollection<Bug> Bugs{ get; set; }
}

并且叶子,花作为虫有自己的属性。
举例:
public class Leaf {
    public Guid LeafId{ get; set; }
    public string Name { get; set; }
    public string Documentation { get; set; }
    public string Description { get; set; }
}

我在我的 View 中使用了部分 View ,因此可以更轻松地使用 ajax 更新它们。
我的正常 View :PlantDetail.cshtml
@model Plant

<table>
<tr>
    <td>
        <h2>@Html.Label(Resources.PlantDetailTitle)</h2>
    </td>
    <td>
        @Html.HiddenFor(m => m.PlantId)
        @Html.DisplayFor(m => m.Name)
    </td>
</tr>
<tr>
    <td>@Html.LabelFor(m => m.Description)
    </td>
    <td>
        @Html.DisplayFor(m => m.Description)
    </td>
</tr>
</table>

 @{Html.RenderPartial("_flowers", Model);}

 @{Html.RenderPartial("_leafs", Model);}

在我的部分 View “_leafs”(以及“_flowers”)中,我有一系列按钮调用需要 LeafId 和 PlantId 的操作:

局部 View “_leafs”:
 @model List<Leaf>

 @for (int i = 0; i < Model.Count(); i++ )
  {
  @(Html.DisplayFor(m => m[i]))
 }

我的显示模板“Leaf.cshtml”:
  @model Leaf
  @Html.HiddenFor(m =>m.LeafId)
   <a class='k-button k-button-icontext' href=@Url.Action("InitiateLeaf", "Plant") +"?    
  leafId=@Model.LeafId&plantId=#=PlantId#">@Model.Name</a>

现在我的问题是我似乎无法在我的显示模板中访问我的父 View 模型的 PlantId。 (而且我在每个显示模板中都有同样的问题..)
我已经在我的 url.action 中使用 routevalues 尝试过它,我知道我最终可以在 javascript 中访问 PlantId,但是有没有任何(mvc)方法可以继续使用 displaytemplates 并且不要将我的 plantId 复制为我的属性子叶 View 模型?

我已经尝试使用类似的方法访问我的 parentviewcontext
"@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()"在我的显示模板中,但似乎没有找到我的 PlantId 的值(如果它甚至存储在那里......)。

还有其他人有一些建议吗?

最佳答案

您提到的一种选择是使用 jquery 访问父 PlantId。如果您需要访问您父级的属性,最好设计您的 View 模型,类似于 Entity Framework 建议我们创建模型类的方式。

public class Plant {
    public Guid PlantId{ get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public ICollection<Leaf> Leafs{ get; set; }
    public ICollection<Flower> Flowers{ get; set; }
    public ICollection<Bug> Bugs{ get; set; }
}

所以你的 Leaf 类也应该有一个导航属性来返回到 Plant。
public class Leaf {
    public Guid LeafId{ get; set; }
    public string Name { get; set; }
    public string Documentation { get; set; }
    public string Description { get; set; }
    public virtual Plant Plant { get; set; }
    public Guid PlantId { get; set; }
}

确保在创建 ViewModel 时填充 Plant 属性和 PlantId。希望这可以帮助

关于asp.net-mvc-4 - 尝试访问显示模板中父 View 模型的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13975397/

相关文章:

c# - 每次使用新的 guid 索引时,如何获取验证消息以呈现在集合属性上?

javascript - 如何获取 child 的数据JavaScript

c# - ASP.NET MVC 3 : Output specific view for concrete implementation

asp.net-mvc - 在asp.net mvc 4的 View 中设置页面标题和元信息

c# - 按相似节点的数量在 MVC View 中显示 XML 节点值?

delphi - DLL 中新窗体的父级设置问题

java - 如何更改旧单元测试的私有(private)抽象父字段

asp.net-mvc - 显示模板未用于界面

c# - MVC DisplayFor 不渲染

asp.net-mvc - 为什么在新 Controller 的 Index 操作上方添加一个空注释(仅 "//")?