简体   繁体   English

将控件绑定到EditorFor模板中的父模型

[英]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. 在此视图中,我正在调用EditorFor并传递子对象。 But in that EditorFor, I want to bind a control of the parent model. 但是在那个EditorFor中,我想绑定父模型的控件。 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 @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 . 如果EditorFor并未使用其作为Model的属性,那么这是一个警告标志,它实际上应该是Partial View,而不是 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. 否则,保持一个EditorFor你必须把父模型对孩子的财产,这是混乱的 ,至少可以说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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