asp.net mvc - Model not binding in POST method -
i have wierd issue mvc. models keeps getting submitted empty. , it's simple, can't find issue.
my model looks this:
public class loginmodel { public string username; public string password; } my controller this:
[httpget] public actionresult login() { return view(); } [httppost] public actionresult login(loginmodel logintest) { if (logintest.username != "x" && logintest.password != "y") { modelstate.addmodelerror("a", "login failed."); return view(logintest); } else { return redirecttoaction("home", "welcome"); } }
and view simple, this.
@model loginsolution.models.loginmodel @{ layout = null; } <html> <head> <meta name="viewport" content="width=device-width" /> <title>login</title> </head> <body> @using (html.beginform("login", "home")) { <div> <span>username : </span> @html.editorfor(model => model.username) <br /> <span>password : </span> @html.editorfor(model => model.password) <br /> @html.validationsummary() <br /> <input type="submit" value="login" name="login" /> </div> } </body> </html> this not question security or best practice. question regarding why model keeps returning empty upon submitting.
your model contains fields, not properties (no getter/setter) model binder cannot set values. change model to
public class loginmodel { public string username { get; set; } public string password { get; set; } }
Comments
Post a Comment