views:

52

answers:

2

For the login page of my website I would like to list the latest news for my site and also display a few fields to let the user log in. So I figured I should make a login view model - I call this LoginVM.

LoginVM contains a Login model for the login fields and a List<NewsItem> for the news listing.

This is the Login model:

public class Login
{

    [Required(ErrorMessage="Enter a username.")]
    [DisplayName("Username")]
    public string Username { get; set; }

    [Required(ErrorMessage="Enter a password.")]
    [DataType(DataType.Password)]
    [DisplayName("Password")]
    public string Password { get; set; }

}

This is the LoginVM view model:

public class LoginVM
{
    public Login login { get; set; }
    public List<NewsItem> newsItems { get; set; }
}

This is where I get stuck. In my login controller, I get passed a LoginVM.

[HttpPost]
public ActionResult Login(LoginVM model, FormCollection form)
{
    if (ModelState.IsValid)
    {
        // What?

In the code I'm checking whether ModelState is valid and this would work fine if the view model was actually the Login model, but now it's LoginVM which has no validation attributes at all.

How do I make LoginVM "traverse" through its members to validate them all? Am I doing something fundamentally wrong using ModelState in this manner?

+1  A: 

In your ViewModel try doing this:

public class LoginVM
{
    [Required]
    public Login login { get; set; }
    public List<NewsItem> newsItems { get; set; }
}
VinTem
This seems to work and I will accept the answer as soon as SO lets me. But what is really happening here? Is this a hack to make ASP.NET MVC validate the `Login` instance?
Deniz Dogan
@Deniz Dogan - Basically if the ModelBinder can't make a new Login(), validation fails. No hacks.
jfar
VinTem
A: 

Alternatively if the newsItems are for display purposes, and don't need validating, then you can pass through just the Login model to your action method.

[HttpPost]
public ActionResult Login([Bind(Prefix = "Login")]Login model)
{
    if (!Model.IsValid)
}

You will also want to use the EditFor and LabelFor helpers in your View.

<%= Html.TextBoxFor(m => m.Login.Username) %>
David Liddle