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

Recommended Answers

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

Jump to Post

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.

Jump to Post

All 6 Replies

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

commented: 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. +1

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.

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.

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.

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.

They match now, right? Case sensitivy?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.