加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-4 – 尝试在displaytemplate中访问父视图模型的属性

发布时间:2020-12-16 09:38:11 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试构建一个asp.net mvc 4应用程序,它使用部分视图和display / editortemplates和Kendo ui. 我有一个特定页面的viewmodel: public class Plant { public Guid PlantId{ get; set; } public string Name { get; set; } public string Description { g
我正在尝试构建一个asp.net mvc 4应用程序,它使用部分视图和display / editortemplates和Kendo ui.
我有一个特定页面的viewmodel:

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,Flower as Bug也有自己的特性.
举例:

public class Leaf {
    public Guid LeafId{ get; set; }
    public string Name { get; set; }
    public string Documentation { get; set; }
    public string Description { get; set; }
}

我在视图中使用了partialviews,因此使用ajax更新它们变得更加容易.
我的正常观点: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);}

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

部分视图“_leafs”:

@model List<Leaf>

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

我的displayTemplate“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>

现在我的问题是我似乎无法在我的displaytemplate中访问我的父Viewmodel的PlantId. (我的每个displaytemplates都有同样的问题..)
我已经在url.action中尝试了routevalues,我知道我最终可以在javascript中访问PlantId,但是有没有(mvc)方法继续使用displaytemplates并且不会将我的plantId复制为我的属性孩子叶子viewmodel?

我已经尝试用类似的东西来访问我的parentviewcontext
“@ HttpContext.Current.Request.RequestContext.RouteData.Values [”controller“].ToString()”在我的displaytemplate中,但似乎找不到我的PlantId的值(如果它甚至存储在那里……).

其他人还有一些建议吗?

解决方法

您提到的一个选项是使用jquery访问父PlantId.如果您需要访问父项的属性,则最好设计类似于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.希望这可以帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读