Asp.net mvc 4 尝试访问displaytemplate中父viewmodel的属性
我正在尝试构建一个asp.NETMVC4应用程序,它使用局部视图、显示/编辑模板和剑道ui。 我有一个特定页面的viewmodel:Asp.net mvc 4 尝试访问displaytemplate中父viewmodel的属性,asp.net-mvc-4,parent-child,display-templates,partialviews,Asp.net Mvc 4,Parent Child,Display Templates,Partialviews,我正在尝试构建一个asp.NETMVC4应用程序,它使用局部视图、显示/编辑模板和剑道ui。 我有一个特定页面的viewmodel: public class Plant { public Guid PlantId{ get; set; } public string Name { get; set; } public string Description { get; set; } public ICollection<Leaf> Leafs{ ge
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; }
}
我在视图中使用partialView,因此使用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);}
@模型植物
@Label(Resources.PlantDetailTitle)
@Html.HiddenFor(m=>m.PlantId)
@DisplayFor(m=>m.Name)
@LabelFor(m=>m.Description)
@DisplayFor(m=>m.Description)
@{Html.RenderPartial(“_flowers”,Model);}
@{Html.RenderPartial(“_leafs”,Model);}
在我的局部视图“\u leafs”(以及“\u flowers”)中,我有一系列按钮,用于调用需要LeafId和PlantId的操作:
局部视图“_leafs”:
@型号列表
@对于(int i=0;im[i]))
}
我的显示模板“Leaf.cshtml”:
@模型叶
@Html.HiddenFor(m=>m.leaveId)
现在我的问题是,我似乎无法在displaytemplate中访问父视图模型的PlantId。(我在每个displaytemplate中都有相同的问题。)
我已经在url.action中使用routevalues尝试过了,我知道我最终可以在javascript中访问PlantId,但是有没有(mvc)方法可以继续使用displaytemplates,并且不将我的PlantId复制为我的子Leaf viewmodel的属性
我已经准备好尝试通过以下内容访问我的parentviewcontext
@HttpContext.Current.Request.RequestContext.RoutedData.Values[“controller”].ToString()”在我的displaytemplate中,但似乎找不到我的PlantId的值(如果它存储在那里的话…)
其他人还有一些建议吗?您提到的一个选项是使用jquery访问父PlantId。如果您需要访问父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; }
}
确保在创建ViewModel时填充Plant属性和PlantId。希望这有帮助找到解决方案了吗?
@model List<Leaf>
@for (int i = 0; i < Model.Count(); i++ )
{
@(Html.DisplayFor(m => m[i]))
}
@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>
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; }
public virtual Plant Plant { get; set; }
public Guid PlantId { get; set; }
}