Question

I'm trying to build an asp.net mvc 4 application that makes use of 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; }
}

And as well Leaf, Flower as Bug have their own properties. By 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'm making use of partialviews in my view so it makes it more easy to update them with ajax. My normal 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 "_leafs" (as well as in "_flowers" I have a serie of buttons that invoke an action that needs the LeafId and the PlantId:

The partial view "_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 can't seem to access the PlantId of my Parent Viewmodel in my displaytemplate. (And I have the same problem in each of my displaytemplates..) I've allready tried it with routevalues in my url.action and I know that I can eventually access the PlantId in javascript, but is there any (mvc) way to keep using displaytemplates and don't duplicate my plantId as a property of my child Leaf viewmodel?

I've allready tried to acces my parentviewcontext with something like "@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()" in my displaytemplate, but don't seem the find the value of my PlantId (if it is even stored there..).

Anyone else still has some suggestions?

Was it helpful?

Solution

One option as you mentioned, is to use jquery to access the parent PlantId. If you have a requirement to access your parent's properties, its better to design your view models similar to how Entity framework recommends us to 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; }
}

So your Leaf class should also have a navigational property to go back to 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; }
}

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top