Reputation: 1205
Here is the situation: I have a view bound to the parent model. In this view, I am calling EditorFor and passing the child object. But in that EditorFor, I want to bind a control of the parent model. How can i do it? I also need to bind the validation control. I created the property in the parent model.
@model ParentModel
//Parent view
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.EditorFor(m => m.Entity, new { CountiesServed = Model.CountiesServed, Types = Model.EntityTypes, CommunicationMethods = Model.CommunicationMethods, OrganizationTypes = Model.OrganizationTypes })
<p class="form-actions" style="text-align:right;">
<button type="submit" >Next »</button>
</p>
}
In the child view - I want to bind a control not to its view model but to the parent view //model. @model ChildModel
@Html.EditorFor(m=>m.somechildentityproperty)
//But how to do something like this?
@Html.EditorFor(//ParentModel property) ???
Upvotes: 1
Views: 729
Reputation: 56429
If the EditorFor
is not using the property used as it's Model, then that's a warning flag that it should actually be a Partial View and not and EditorFor
. That way you can easily pass in the parent model.
Otherwise, keeping an EditorFor
you'd have to put the Parent model on the child property, which is messy to say the least.
Upvotes: 1