ASP.net MVC2. Populated model is not getting back

2019-08-26 19:48发布

问题:

Say I input 'email@domain.com' 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:

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; }
}


回答2:

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.

Using the examples above. What I needed to change was the argument for Login() to use the whole model that was being used by the view.

...

using FCX.Models.Auth;

...

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

        ...