asp.net-mvc-4 – 尝试在displaytemplate中访问父视图模型的属性
我正在尝试构建一个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更新它们变得更加容易. @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都有同样的问题..) 我已经尝试用类似的东西来访问我的parentviewcontext 其他人还有一些建议吗? 解决方法
您提到的一个选项是使用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.希望这可以帮助 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – ASP.NET MVC中的非字符串角色名称?
- asp.net – Web配置转换不工作
- asp.net-mvc – 使用Multipart格式的Web API模型绑定
- ASP.NET requestValidation 4.5和WIF
- asp.net-mvc – 将SignalR与现有授权集成
- asp.net – SQL使用逗号分隔值和IN子句
- asp.net – 当passwordFormat = Encrypted和decryption = A
- ASP.NET MVC 3 – Ajax.BeginForm vs jQuery Form Plugin
- asp.net – 跳过CustomErrors并转到HttpErrors
- asp.net-mvc – 如何重定向HTTP到HTTPS在MVC应用程序(IIS7.
- asp.net – 异步代码,无需等待完成
- entity-framework – 如何在ASP.NET MVC 5中为同
- 在ASP.NET RadioButtonList ListItem上设置CSS类
- telerik – ASP.NET MVC2富文本编辑器
- asp.net-mvc-4 – 使用StructureMap为NServiceBu
- 学习ASP.NET的第一天
- asp.net – 无法完成操作.提供的SqlConnection不
- asp.net-mvc-3 – 在图表.net mvc3中设置间隔
- ASP.net MVC中的Ajax帮助器
- asp.net-web-api – ASP.NET Core中的IHttpActio