简体   繁体   中英

binding a control to parent model in EditorFor template

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 &#187;</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) ??? 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM