MVC4 - Partial View Model binding during Submit

前端 未结 7 2235
野趣味
野趣味 2021-02-19 19:29

I have view model which has another child model to render the partial view (below).

public class ExamResultsFormViewModel
{
    public PreliminaryInformationView         


        
相关标签:
7条回答
  • 2021-02-19 19:56

    For .net core 2 and mvc, use do like below:

    @{
     Html.ViewData.TemplateInfo.HtmlFieldPrefix = "Contact"; 
    }
     @await Html.PartialAsync("_YourPartialViewName", Model.Contact)
    
    0 讨论(0)
  • 2021-02-19 19:57

    You can add the HtmlFieldPrefix to the top of your partial view:

    @{
        ViewData.TemplateInfo.HtmlFieldPrefix = "Contact";
    }
    

    This is the same approach as that described by @cpoDesign but it means you can keep the prefix in your partial view if you need to do that.

    0 讨论(0)
  • 2021-02-19 20:00

    I also ran into this problem. I will explain my solution using your code. I started with this:

    @{
       Html.RenderPartial("_PreliminaryInformation", Model.PreliminaryInformation);
    }
    

    The action corresponding to the http post was looking for the parent model. The http post was submitting the form correctly but there was no reference in the child partial to the parent partial. The submitted values from the child partial were ignored and the corresponding child property remained null.

    I created an interface, IPreliminaryInfoCapable, which contained a definition for the child type, like so:

    public interface IPreliminaryInfoCapable
    {
        PreliminaryInformationViewModel PreliminaryInformation { get; set; }
    }
    

    I made my parent model implement this interface. My partial view then uses the interface at the top as the model:

    @model IPreliminaryInfoCapable
    

    Finally, my parent view can use the following code to pass itself to the child partial:

    @{
        Html.RenderPartial("ChildPartial", Model);
    }
    

    Then the child partial can use the child object, like so:

    @model IPreliminaryInfoCapable
    ...
    @Html.LabelFor(m => m.PreliminaryInformation.ProviderName)
    etc.
    

    All of this properly fills the parent model upon http post to the corresponding action.

    0 讨论(0)
  • 2021-02-19 20:04

    I know its a bit late but it might help to someone

    If you have complex model, you can still pass it into partial using:

    @Html.Partial("_YourPartialName", Model.Contact, new ViewDataDictionary()
    {
        TemplateInfo = new TemplateInfo()
        {
            HtmlFieldPrefix = "Contact"
        }
    })
    

    where I have defined model with property "Contact". Now what HtmlFieldPrefix do is add the property binding for each model "so the model binder can find the parent model"

    There is a blog post about it: http://www.cpodesign.com/blog/bind-partial-view-model-binding-during-submit/

    .NET Core 2 binding

    In .NET Core 2 and MVC the answer above will not work, the property is no longer settable.

    How ever the solution is very similar.

     @{ Html.ViewData.TemplateInfo.HtmlFieldPrefix = "Contact"; }
     @await Html.PartialAsync("_YourPartialName", Model.Contact)
    

    after you can submit your model, it will bind again.

    Hope that helps

    0 讨论(0)
  • 2021-02-19 20:08

    Quick Tip: when calling your EditorFor method, you can set the name of the template as a parameter of the Html.EditorFor method. Alternatively, naming conventions can be your friend; just make sure your editor template filename is exactly the same name as the model property type

    i.e. model property type 'CustomerViewModel' => 'CustomerViewModel.cshtml' editor template.

    0 讨论(0)
  • 2021-02-19 20:11

    Please make the below changes to your partial page. so it will come with your Parent model

    //Parent Page
    @{Html.RenderPartial("_PreliminaryInformation", Model.PreliminaryInformation);}
    
    //Partial Page
    @model Web.Models.Preliminary.PreliminaryInformationViewModel
    @using (Html.BeginCollectionItem("PreliminaryInformation", item.RowId, true))
        {
    <div>
    @Html.TextBoxFor(x => x.DateOfService })
    </div>
    
    }
    
    0 讨论(0)
提交回复
热议问题