Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

c# - View with model that has children that act as model for strongly typed partial views

I have a system consisting of main page that starts a form:

@model Stuvo_Intakeformulier.Models.CaterRequest

@using (Html.BeginForm(null, null, FormMethod.Post, new { id="fooData", @class="form-horizontal", role="form"}))
        {
         <div id="generalInfo" style="display:none">
              @{Html.RenderPartial("Food", Model.foodInfo);}
         </div>

         <div id="studyInfo" style="display:none">          
               @{Html.RenderPartial("Drinks", Model.drinkInfo);}
               <input type="submit" value="Bevestigen">
        ....

using a parent model

public class CaterRequest
{
    public CaterRequest()
    {
        foodInfo = new Food();
        drinkInfo = new Drinks();
    }

   public Food foodInfo { get; set; }
   ....

And child models like this:

public class Food
{
    [Required(ErrorMessage = "BlaBla")]
    public string name { get; set; }
}

Finally, the partial view looks like this:

@model Stuvo_Intakeformulier.Models.Food

<div class="form-group">    
        @Html.LabelFor(model => Model.name, "Naam: ", htmlAttributes: new { @class = "col-sm-2 control-label" })
        <div class="col-sm-10">
            @Html.TextBoxFor(model => Model.name, null ,htmlAttributes: new { @class = "form-control" })
        </div>
        <div class="col-sm-offset-2 col-sm-10">
            @Html.ValidationMessageFor(model => Model.name)
        </div>
    </div>

Obviously when posting my model.foodInfo.name will be empty because of the fact that in my partial views the name attribute is name instead of foodInfo.name. I can solve this by changing @Html.TextBoxFor(model => Model.name.. with @Html.TextBox('foodInfo.name' but then I lose all the advantages of my strongly typed partial view (especially the client side validation, which is something I absolutely need to have).

I was wondering what the best solution to this problem would be? Is there an easy way to solve this? What is the cleanest solution?

Basically I want to bind the data from my strongly typed partial views to my model in my normal view, while still maintaining the strongly typed views and validation stuff.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We use DisplayFor and EditorFor template. Its clean and comes with all features you mentioned. We have working code that is quite similar to this link.

So in you example above, you will need to create a Food.cshtml that can contain similar to what you have:

@model Stuvo_Intakeformulier.Models.Food

    <div class="form-group">    
                @Html.LabelFor(model => Model.name, "Naam: ", htmlAttributes: new { @class = "col-sm-2 control-label" })
                <div class="col-sm-10">
                    @Html.TextBoxFor(model => Model.name, null ,htmlAttributes: new { @class = "form-control" })
                </div>
                <div class="col-sm-offset-2 col-sm-10">
                    @Html.ValidationMessageFor(model => Model.name)
                </div>
</div>

Above Food.cshtml file has to be placed into the “Shared/EditorTemplates/” folder.

Then in your main page you can call like:

 <div id="generalInfo" style="display:none">
      @Html.EditorFor(m => m.foodInfo)
 </div>

As a aside, Do you have a typo in public string foodInfo { get; set; }, it should be public Food foodInfo { get; set; }?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...