开发者

Error when calling Html.Partial("LogOnPartial") in template MVC 3 project

开发者 https://www.devze.com 2023-02-08 10:55 出处:网络
Everything works fine out of the box, but after adding a new controller that returns a strongly typed view using the model \'MySite.Models.Cars\', i get the an error when calling Html.Partial(\"LogOnP

Everything works fine out of the box, but after adding a new controller that returns a strongly typed view using the model 'MySite.Models.Cars', i get the an error when calling Html.Partial("LogOnPartial") in SiteLayout.cshtml. I call the new view like this:

return View(db.Cars.FirstOrDefault());

This is the error:

The model item passed into the dictionary is of type 'MySite.Models.Cars', but this dictionary requires a model item of type 'MySite.Models.LogOnMod开发者_开发知识库el'.

Very frustrating. When i use the new view without supplying a model it works again.

return View();

I setup the exact same conditions in MVC 2/ASPX and it worked just fine. I am not sure whether this is an MVC 2/MVC 3 or ASPX/Razor issue. In fact it might be a late night issue...

Very thankful for any reply.

EDIT:

First line from LogOnPartial.cshtml (i removed the underscore)

@model MySite.Models.LogOnModel

First line from Index.cshtml (new view):

@model MySite.Models.Cars


By default the _LogOnModel.cshtml partial as created by the template is not strongly typed and doesn't require any model. If you modified it so that it requires a model you will need to pass this model when calling it:

@Html.Partial("_LogOnPartial", SomeLogonModelInstance)


Post the code from your View. (atleast the first few lines). Your view is expecting a Model of a certain type (LogOnModel) but you're passing in a different type (Cars).

You should see this LogOnModel referenced in the first line of your View. If you see the error, go ahead and fix it, if not post back here with a copy of your view and we'll help you out.


It seems as though it might be a late night issue. What is the type of the partial? From what the error reads it seems as though you are passing in a Model type of Cars when the partial is looking for a Model of LogOn. This could have been something that was automatically created when you created the Partial.


It seems you're using Custom Membership and a partial view (_LogOnPartial) in _Layout page. By default _Layout page does not have a controller so don't use any model on it if is not necessary. Put your LogOnModel out of the _Layout page and let LogOff and LogOn ActionLikns remain on _LogOnPartial just like EF default membership approach.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号