22

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>

}

tereško
  • 58,060
  • 25
  • 98
  • 150
user3541362
  • 329
  • 2
  • 8
  • 16
  • For those who want to know how to solve this question in .NET Core 2.1 through 3.1, see https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/partial-tag-helper?view=aspnetcore-3.1. – Mike Grove aka Theophilus Aug 19 '20 at 19:02

3 Answers3

35

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
alexo
  • 936
  • 8
  • 16
  • If you have one that has to get data from a DB, do something complex, etc then i still can use `RenderPartial` by **just map data to the Model property** , check [this](http://stackoverflow.com/a/17627843/2218697) – Shaiju T Oct 03 '16 at 11:45
12

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>
 }
2

This is quite simple to do. There is a html directive which can render a partial view. Following is the code sample:

 @Html.Partial("nameOfPartial", Model)

Now here Model could be from your main controller.

or you can define a new controller action with partialviewresult as return type and try to render it in the page like this:

@{Html.RenderAction("Someaction", "somecontroller");}
qamar
  • 1,437
  • 1
  • 9
  • 12
  • 2
    @Html.Partial("nameOfPartial", Model) giving a model like that does not work. it still clashes with the model in the main view. the partial view contains a form, will the renderaction method get the post when i am using that? – user3541362 May 11 '14 at 15:32
  • I am not sure if render action shall work or not. But you can try that. the only diff will be renderaction will load an action which returns a partialviewresult instead of viewresult. try that and see if that works – qamar May 11 '14 at 15:34
  • well to save the entry you need to have a post method in your partial view. do you have that? – qamar May 11 '14 at 15:49
  • yes i do. I have a get controller and a post controller. – user3541362 May 11 '14 at 15:51
  • And you have no submit button in your partial view? How do expect it to submit values? – qamar May 11 '14 at 16:00
  • i have a submit in my main view. I want it to use that submit not its own. – user3541362 May 11 '14 at 16:00
  • Now I get that. Well I am still not sure then why do you have a form tag in your partial view? One option here now, do a jquery post to you r main controller. get all the values via fieldname. there are plenty of examples of jquery post. and also you need to update your mailviewmodel to add an additional field to accomodate value of partial view. and get rid of those form tag from partial view – qamar May 11 '14 at 16:04