I'm trying to set up an in page login form and I'm getting complaints of receiving the wrong model. I've edited the default _LogOnPartial file with my own code that has a login form and told it to accept a login model. Works fine on the main page but as
soon as you go to another page that accepts another model, it claims it receives the wrong one. I've read online that you can wrap models into a wrapper model which sounds fine but I can't figure out how to make the _LogOnPartial file know the right model
to access.
For example:
public class AdminPage
{
public LogonModel login {get; set; }
public List<User> users { get; set; }
}
public class HomePage
{
public LogonModel login {get; set; }
}
How can I know how to make _LogOnPartial accept a HomePage model vs an AdminPage model?
Edit: I ended up ditching the model in LogOnPartial
public interface ILogon
{
LogonModel logon {get; set;}
}
public class _BaseModel : ILogon
{
public LogonModel logon {get; set;}
}
public class AdminPage : _BaseModel
{
....
}
public class HomePage : _BaseModel
{
....
}
then in the partial _LogoOnPartial view the model is of type ILogon
I really don't understand what purpose the interface is serving in your code sample. It looks like you're using basic inheritance to ensure both viewmodels have a LogonModel property. This can get quite messy quite quickly. Inheritance is the tightest form
of coupling and should only be used when there is exactly the same behaviour expected. The viewmodels you describe have no behaviour and are simple data structures. I would avoid using inheritance for this. What you're getting at best is less typing and at
worst a very rigid coupling between viewmodels. Inheritance causes all preconditions, postconditions and invariants to be inherited. Don't think lightly of it.
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
I guess I just realized that I'm trying to use the default layout for now and will build upon it and change it. So right now, the @Html.Partial("LogOnPartial") is located inside of the _Layout.cshtml file that contains the general layout of the website.
So it doesn't appear that I can pass in any Models when calling @Html.Partial() because the default _Layout.cshtml does not accept any models itself.
dhatch6
Member
3 Points
2 Posts
How to use multiple models on a page with asp.net mvc 3
Oct 29, 2011 08:42 PM|LINK
I'm trying to set up an in page login form and I'm getting complaints of receiving the wrong model. I've edited the default _LogOnPartial file with my own code that has a login form and told it to accept a login model. Works fine on the main page but as soon as you go to another page that accepts another model, it claims it receives the wrong one. I've read online that you can wrap models into a wrapper model which sounds fine but I can't figure out how to make the _LogOnPartial file know the right model to access.
For example:
public class AdminPage { public LogonModel login {get; set; } public List<User> users { get; set; } }public class HomePage { public LogonModel login {get; set; } }How can I know how to make _LogOnPartial accept a HomePage model vs an AdminPage model?
Edit: I ended up ditching the model in LogOnPartial
HeartattacK
All-Star
55288 Points
5920 Posts
Moderator
MVP
Re: How to use multiple models on a page with asp.net mvc 3
Oct 29, 2011 08:50 PM|LINK
Are you trying to pass a model to a partial? If you, you can pass it as the second parameter:
@Html.Partial("Login.cshtml", Model.ThePropertyYouWishToBeTheModelForThePartial)
Does that help?
blog: www.heartysoft.com
twitter: @ashic
bruce (sqlwo...
All-Star
37626 Points
5574 Posts
Re: How to use multiple models on a page with asp.net mvc 3
Oct 29, 2011 08:50 PM|LINK
use intefaces, thats what they were designed for:
public interface ILogon { LogonModel logon {get; set;} } public class _BaseModel : ILogon { public LogonModel logon {get; set;} } public class AdminPage : _BaseModel { .... } public class HomePage : _BaseModel { .... } then in the partial _LogoOnPartial view the model is of type ILogonHeartattacK
All-Star
55288 Points
5920 Posts
Moderator
MVP
Re: How to use multiple models on a page with asp.net mvc 3
Oct 29, 2011 08:56 PM|LINK
I really don't understand what purpose the interface is serving in your code sample. It looks like you're using basic inheritance to ensure both viewmodels have a LogonModel property. This can get quite messy quite quickly. Inheritance is the tightest form of coupling and should only be used when there is exactly the same behaviour expected. The viewmodels you describe have no behaviour and are simple data structures. I would avoid using inheritance for this. What you're getting at best is less typing and at worst a very rigid coupling between viewmodels. Inheritance causes all preconditions, postconditions and invariants to be inherited. Don't think lightly of it.
blog: www.heartysoft.com
twitter: @ashic
dhatch6
Member
3 Points
2 Posts
Re: How to use multiple models on a page with asp.net mvc 3
Oct 29, 2011 09:50 PM|LINK
I guess I just realized that I'm trying to use the default layout for now and will build upon it and change it. So right now, the @Html.Partial("LogOnPartial") is located inside of the _Layout.cshtml file that contains the general layout of the website. So it doesn't appear that I can pass in any Models when calling @Html.Partial() because the default _Layout.cshtml does not accept any models itself.
HeartattacK
All-Star
55288 Points
5920 Posts
Moderator
MVP
Re: How to use multiple models on a page with asp.net mvc 3
Oct 30, 2011 08:32 AM|LINK
You can pass in the "extra" bits via ViewBag. In your _Layout, you can do something like:
@if(ViewBag.ModelForPartial != null)
{
<text>@Html.RenderPartial("Login", ViewBag.ModelForPartial)</text>
}
And in your login, you can declare @model TypeOfModelForPartial
Since it's data not relevant to a particular view, passing it via ViewBag is a good option.
blog: www.heartysoft.com
twitter: @ashic