views:

47

answers:

2

Say I input '[email protected]' and 'password' into the login fields.

The data annotations in AuthModel.cs throw back that the values aren't populated. So the data in the html form is never actually populating the model thats being thrown around.

In my awesomeness, I made a lot of hurried changes yesterday before closing the solution and didn't test the changes I had made. As a result, I don't know what I changed to break this.

If I need to post anything else let me know.

AuthController.cs

...

using FCX.Models.Auth;

...

// GET: /auth/login
[HttpPost]
public ActionResult Login(Login model)
{
    if (ModelState.IsValid)
    {
        ...

AuthModel.cs

public class AuthModel
{
    public Login Login { get; set; }
    public ForgotPassword ForgotPassword { get; set; }
    public Register Register { get; set; }
}

...

public class Login
{
    [Required
        (ErrorMessageResourceName = "Login_Error_HandleRequired",
         ErrorMessageResourceType = typeof(Resources.Auth))]
    [Email(ErrorMessage="Invalid Email")]
    public string Handle { get; set; }
    [Required
        (ErrorMessageResourceName = "Login_Error_PasswordRequired",
         ErrorMessageResourceType = typeof(Resources.Auth))]
    public string Password { get; set; }
    public bool Memory { get; set; }
}

And the Index.aspx that is the login form.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Templates/FullColumn.Master" Inherits="System.Web.Mvc.ViewPage<FCX.Models.Auth.AuthModel>" %>

...

<% Html.EnableClientValidation(); %>
<%=Html.ValidationSummary() %>
<% using (Html.BeginForm("login", "auth"))
   { %>
<div id="login-block" class="entry-block">
    <h4>
        Sign in</h4>
    <ul>
        <li>
            <label for="Login_Handle">
                Email Address</label><%=Html.TextBoxFor(Model => Model.Login.Handle, new { @class = "txt-input" })%></li>
        <li>
            <label for="Login_Password">
                Password</label><%=Html.PasswordFor(Model => Model.Login.Password, new { @class = "txt-input" })%></li>
        <li>
            <%=Html.CheckBoxFor(Model => Model.Login.Memory, new { @class = "left-input" })%><label
                for="Login_Memory" class="right-label">Keep me logged in</label></li>
        <li>
            <button type="submit" name="Login.Submit" value="1">
                Sign in</button></li>
    </ul>
</div>
<% } %>
+1  A: 

Should those properties be of type string and bool?

public class AuthModel
{
    public ->string<- Login { get; set; }
    public ->string<- ForgotPassword { get; set; }
    public ->bool<- Register { get; set; }
}
Jakub Konecki
Nope, thats a model to hold the smaller models. AuthModel is used by the view to help with displaying the form elements.
castis
A: 

After looking over this for some time I figured it out.

I was giving the view the entire model (which included the Login, Register, and Forgot Password models) and then submitting the whole model (only 1 of which was populated). And then when the controller was calling for the model that was passed in, it was only looking for one of the models inside the bigger model.

I got around that originally by only passing in pieces to the view and returning them properly. When I changed around my code, I had broken the previously working way.

castis
Can you post the corrected code for future visitors?
MattC