0

I have a view "Register Application", containing 20 fields. The user registers. After user registered I have an option for user to update components that were submitted.

for example: view Register has sections "Contact Information" , "Address Information" ...etc currently have 5 sections.

so in the Register view I create a viewmodel @model ViewModels.RegisterVMwith all the fields.

and for edit contact information, I have its own @model ViewModels.ContactInformationVM

Now, here is my question since both views will have the same markup code I decided to create a partial view for Contact Information so I can reuse the markup code and will be able to manage it in one place instead of two places.

So in the Register view

 @model ViewModels.RegisterVM
  .......
 @Html.Partial("~/Views/Shared/widget/_ContactInformation.cshtml", @Model)

and in the Contact Information view I want to reuse this partial view

 @model ViewModels.ContactInformationVM` 
 @Html.Partial("~/Views/Shared/widget/_ContactInformation.cshtml", @Model)

Both views have its own viewmodel and the partial view will only be able to accept one viewmodel

No idea what viewmodel I should declare in the partial view

I know I can just copy the code from the partial view and place in the Register view and in the Contact Information view and it would work and solve the issue.. but was wondering if there a better approach to avoid having same code in multiple files.

I hope it makes sense what I am asking. Thanks for reading.

4
  • I would highly recommend using ajax calls to put the partial on the view. see my answer here stackoverflow.com/questions/18847735/… Commented Mar 26, 2015 at 20:34
  • If I'm understanding the question correctly, then I'm confused. A view model is used to support a view. If you're using the same [partial] view, then they should be using the same view model. Keeping in mind that a view model is NOT a domain model -- if they have the same data then they are likely the same model; they don't have a semantic difference like domain objects might. Rename the model ContactInformation and use that. Commented Mar 26, 2015 at 20:56
  • @MattBodily: How does pulling in the partial via AJAX solve anything here? Commented Mar 26, 2015 at 21:01
  • @ChrisPratt much simpler to do an ajax call to load the partial view than alter the view model on every view and try to pass that information around. in my opinion anyway Commented Mar 26, 2015 at 21:23

1 Answer 1

1

What you want to do here is create a composite view model that has each section as a separate property. For example:

public class RegisterVM
{
    public ContactInformationVM ContactInformation { get; set; }
    public AddressInformationVM AddressInformation { get; set; }
    ...
}

Then in your partial(s), you reference the sub-model:

_ContactInformation.cshtml

@model ViewModels.ContactInformationVM

<!-- contact info fields here, for example: -->
@Html.EditorFor(m => m.FirstName)
...

Then, in your register view, you use RegisterVM as your model and load the partials for each section:

@model ViewModels.RegisterVM

@Html.Partial("_ContactInformation", Model.ContactInformation)
@Html.Partial("_AddressInformation", Model.AddressInformation)
...

Now, you can reuse these components at will.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Chris. When I use the code you provided I get an error message on line where you add @Html.Partial("_ContactInformation", Model.ContactInformation) Error on runtime: Object reference not set to an instance of an object. Do you know why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.