3

I have two views: 1) Register – primary view for creating user account 2) Category – partial view for adding category dynamically if it's not there in a register view combo field.

The Category view is shown as jQuery dialog when user click on Add Category while registering. This view shows fields required to make up a new category like name and description. For this reason, it has a separate model.

On get page everything works fine, however on post, if there's some validation error, the page needs to reload with user provider values for making correction, instead it throws following error:

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'Delight.Models.User', but this dictionary requires a model item of type 'Delight.Models.Category'.

I thought that using the following statement might be causing this problem (since it doesn't specify the model object to use for partial view):

@Html.Partial("CreateCat")

However, resorting to the following overload didn't solve the problem either:

@Html.Partial("CreateCat", null, null)

Above the second parameter (with null value) represents the model object.

However, unexpectedly following solved my problem:

@Html.Partial("CreateCat", new Category(), null)

Why is empty object working in this case but null isn't?

Is there any other better way to render strongly-typed partial view with different model type.

6
  • I don't think this is really the way the MVC pattern is supposed to be used... Perhaps your partial view should be integrated into your page or on a separate page? Aug 12, 2011 at 17:13
  • @Jeremy I don't have other option. The category is added via ajax and I don't like to open a new browser window for user to do this. Actually it will never require any post back data. btw, What's the problem here regarding MVC? Is MVC not supposed to give us more control over how we structure different components?
    – Varun K
    Aug 12, 2011 at 17:38
  • Because if you pass a null object to a view it will crash on you (if you don't first check for a null model). Calling Html.Partial("CreateCat") is passing the parents model (Models.User). Calling it with the new Category() I guess is essentially doing what the controller would do when returning an empty, new Category to create. Edit: I may be wrong on this as I am new to MVC, but this is from what I understand.
    – Jack
    Aug 12, 2011 at 17:40
  • 2
    It sounds like your Add Category method should be an ajax call, not a partial view. The only thing you want to change in your DOM is adding another option to your combo box right? your jQuery post variables should match your CategoryAdd model, and that should return a JsonResult depicting any information you need to add the dropdown item to the DOM and make the SELECT value consumable. Aug 12, 2011 at 17:49
  • @Jeremy this is exactly what I'm doing. I have separated the category into a partial view to make code more cleaner. The partial view is rendered (hidden) only at the time of full page load. When user clicks on add link (before category combo), it gets appeared as a a dialog. User specifies name and description and post occurs via ajax, dialog disappears and new categoryId + name is appended to the original combo.
    – Varun K
    Aug 12, 2011 at 18:47

1 Answer 1

3

Unless you want to use the parent's model or no model, you should always pass in the appropriate model for a partial view. Passing in null is silly, if you don't want an instantiated model in your partial view then why would your partial view use a model at all? In this instance, I think new Category() is the correct choice. However, I've always seen sub models passed into partials

@Html.Partial("CreateCat", Model.Category)

something about needing to do what you're doing seems strange, but I'm not certain how you're using the partial view.

3
  • I need partial view because the category is itself a separate model (albeit very small one) with its validator like stringlength and so on. As I said in comments (see above) that although this can be done by opening a new browser window (to avoid partial views), I want to make it work with jquery dialog and with html preloaded for smooth user experience.
    – Varun K
    Aug 12, 2011 at 18:53
  • Model.Category looks alright to me but I think I can stick to my solution as I said the category view will always be empty during full page loads. Thanks for your answer.
    – Varun K
    Aug 12, 2011 at 18:58
  • I don't agree " always pass in the appropriate model for a partial view". For example, my partial view is generate a MemberID DropDownList, the Model of view is definitely "Members". It may declare the model on top of view by @model keyword.
    – Cheung
    Mar 15, 2013 at 1:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.