0

When I click the login button I never get any values posted to the server. I accept a AuthViewModel but it is always null. I have tried using @html.EditorFor but have not had any luck with it.

User Controller

        [HttpGet]
        public ActionResult Password()
        {
            UserModel user = (UserModel)Session[Constants.SessionVars.Authenticate];
            //error - need a profile
            if (user == null)
                return Redirect("/User/Login");

            return View(new AuthViewModel());
        }

        [HttpPost]
        public ActionResult Password(AuthViewModel password)
        {
            UserModel user = (UserModel)Session[Constants.SessionVars.Authenticate];
            //error - need a profile
            if (user == null)
                return Redirect("/User/Login");

            if (password == null || string.IsNullOrEmpty(password.Password))
            {
                ViewBag.Error = Constants.ErrorMessages.UserPassword_PassBlank;
                return View(new AuthViewModel());
            }

            var dataContext = new UserdbDataContext();
            int code = dataContext.AuthenticateAdmin(user.Id, password.GetHashCode());

            //check if password matches
            if (code != 1)
            {
                ViewBag.Error = Constants.ErrorMessages.UserPassword_WrongPass;
                return View(new AuthViewModel());
            }

            //success
            Session[Constants.SessionVars.User] = user;
            Session.Remove(Constants.SessionVars.Authenticate);
            return Redirect("/");
        }

User Model

    public class AuthViewModel
    {
        public string Password { get; set; }
    }

View

@model MvcApplication1.Models.AuthViewModel
@{
    ViewBag.Title = "Password";
}

<h2>Password</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div>@Html.TextBoxFor(m => m.Password,new{placeholder="Password",type="password",autofocus=""})</div>
    <div><button id="btnLogin" type="submit">Login</button></div>
    <div class="error">@ViewBag.Error</div>
}
2
Contributors
6
Replies
27
Views
4 Years
Discussion Span
Last Post by ggamble
1

Try FormCollection in your controller and see if anything is coming through that way.

Edited by ggamble

Votes + Comments
That works but how could I make it work with a model? I have another page that is almost identical and it passes back a model like I would expect.
0

That works but how could I make it work with a model? I have another page that is almost identical and it passes back a model like I would expect.

0

If your doing any async postbacks prior to the submit, then the model will be emptied after the postback, unless you re-popluate it on each ajax call.

Edited by ggamble

0

There is no ajax on the page. All I want is that one textbox to send me the result and then I will redirect to somewhere else.

0

I managed to get it to submit the password by changing the parameter name on the controller... I have no idea why that would that make a difference.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.