Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass a different model to the partial view

Tags:

I am trying to pass a different model to the partial view from a view. I have two separate controller actions for both of them and two different view models. But when I call the partial view from within the view it gives me the error

The model item passed into the dictionary is of type 'Application.ViewModels.Model1ViewModel', but this dictionary requires a model item of type 'Application.ViewModels.PartialViewModel'.

I am calling it like this:

 @Html.Partial("_CreateUniFunctionPartial") 

the model call in the view is

@model Application.ViewModels.Model1ViewModel 

and model in partial view file is

@model Application.ViewModels.PartialViewModel 

I am not sure how to pass the partial view so it doesnt give this error.

EDIT

Partial view

@model Application.ViewModels.PartialViewModel     @using (Html.BeginForm("partialview", "ApplicationDetail", FormMethod.Post))    {   <div class="form-horizontal">     <h4>UniFunctionViewModel</h4>     <hr />     @Html.ValidationSummary(true)          <div class="form-group">         @Html.LabelFor(model => model.detail, new { @class = "control-label col-md-2" })         <div class="col-md-10">             @Html.TextBoxFor(model => model.detail, new { @placeholder = "Further Information" })             @Html.ValidationMessageFor(model => model.detail)         </div>     </div>   </div> 

}

like image 344
user3541362 Avatar asked May 11 '14 15:05

user3541362


People also ask

Can we use model in partial view?

Partial Views can use the Page Model for their data whereas Child Actions use independent data from the Controller. Editor/Display templates pass items from the model to the system but can be overridden by user partial views.

How does a partial view support a model?

It helps us to reduce code duplication. In other word a partial view enables us to render a view within the parent view. The partial view is instantiated with its own copy of a ViewDataDictionary object which is available with the parent view so that partial view can access the data of the parent view.

How do you pass models in view?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.


2 Answers

you are using the right method but not passing in the right arguments

you might want to try it like this:

@Html.Partial("~/[path_to_root_only_if_exists]/_CreateUniFunctionPartial.cshtml", new Application.ViewModels.PartialViewModel()) 

if you do not pass in a model, it will automatically take the one from its parent, which in your case is

Application.ViewModels.Model1ViewModel 
like image 157
alexo Avatar answered Oct 03 '22 02:10

alexo


One thing you will need to do is regenerate a model or utilize a property in the model. For example:

 public class OuterViewModel  {      public InnerViewModel InnerViewModel { get; set; }  }   public class InnerViewModel  {      public string SomeProperty { get; set; }  } 

In the top page, you can accept the OuterViewModel, then pass the InnerViewModel to the Partial.

Outer.cshtml:

 @model OuterViewModel  @Html.Partial("_InnerPartial", Model.InnerViewModel) 

_InnerPartial.cshtml:

 @model InnerViewModel  @using (Html.BeginForm("Inner", "Controller"))  {       <div>           @Html.AntiForgeryToken()           @Html.TextBoxFor(m => m.SomeProperty)           <input type="submit" value="Save" />       </div>  } 
like image 36
Zachare Sylvestre Avatar answered Oct 03 '22 04:10

Zachare Sylvestre