Try opening the property of the parent view model in the displaytemplate

I am trying to create an asp.net mvc 4 application that uses partial views and display / editortemplates and Kendo ui. I have a viewmodel for a specific page:

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; } } 

As well as Leaf, Flower as an error have their own properties. Example:

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

I use partialviews in my view, so simplify updating them with ajax. My usual 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);} 

In my partial view of "_leafs" (and also in "_flowers", I have a series of buttons that trigger an action that requires LeafId and PlantId:

Partial view of "_leafs":

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

My 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> 

Now my problem is that I cannot access the PlantId of my parent view model in my displaytemplate. (And I have the same problem in each of my displaytemplates ..) I already tried it with routevalues ​​in url.action, and I know that in the end I can access PlantId in javascript, but is there any way ( mvc) continue to use displaytemplates and not duplicate my plantId as a property of my embossed elevator model?

I already tried to use my parent context with something like "@ HttpContext.Current.Request.RequestContext.RouteData.Values ​​[" controller "]. ToString ()" in my displaytemplate, but it doesn't seem like you find the value of mine PlantId (if it is even stored there ..).

Anyone else have any suggestions?

+4
source share
1 answer

One option, as you mentioned, is to use jquery to access the parent PlantId. If you have a requirement to access parent properties, it is best to design your view models similar to how the Entity structure recommends that we create model classes.

 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; } } 

Thus, your Leaf class must also have a navigation property in order to return to the factory.

 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; } } 

Make sure you create the Plant property and PlantId when creating the ViewModel. Hope this helps

+1
source

All Articles