asp.net mvc - MVC ModelBinding cannot get the label text to bind to a string property -
i'm trying follow pattern presented in following post:
how asp.net mvc model generate input names?
and i've run problem. i've copied code own application shown, when form posted model has null rolename property of roles collection. values posted checkboxes correct, rolename not binding.
below code i'm using:
register.cshtml
@model simplemembershiptest.models.registerviewmodel @{ viewbag.title = "register"; } <hgroup class="title"> <h1>@viewbag.title.</h1> <h2>create new account.</h2> </hgroup> @using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary() <fieldset> <legend>registration form</legend> <ol> <li> @html.labelfor(m => m.username) @html.textboxfor(m => m.username) </li> <li> @html.labelfor(m => m.firstname) @html.textboxfor(m => m.firstname) </li> <li> @html.labelfor(m => m.lastname) @html.textboxfor(m => m.lastname) </li> <li> @html.labelfor(m => m.password) @html.passwordfor(m => m.password) </li> <li> @html.labelfor(m => m.confirmpassword) @html.passwordfor(m => m.confirmpassword) </li> <li> @html.labelfor(m => m.email) @html.textboxfor(m => m.email) </li> </ol> <div class="rolecheckboxes"> @html.editorfor(m => m.roles) </div> <input type="submit" value="register" /> </fieldset> } @section scripts { @scripts.render("~/bundles/jqueryval") }
registerviewmodel.cs
namespace simplemembershiptest.models { public class registerviewmodel { [required] [display(name = "user name")] public string username { get; set; } [required] [display(name = "first name")] public string firstname { get; set; } [required] [display(name = "last name")] public string lastname { get; set; } [required] [stringlength(100, errormessage = "the {0} must @ least {2} characters long.", minimumlength = 6)] [datatype(datatype.password)] [display(name = "password")] public string password { get; set; } [datatype(datatype.password)] [display(name = "confirm password")] [compare("password", errormessage = "the password , confirmation password not match.")] public string confirmpassword { get; set; } [required] [emailaddress] [display(name = "email")] public string email { get; set; } public ienumerable<roleviewmodel> roles { get; set; } } }
roleviewmodel.cs
@model simplemembershiptest.models.roleviewmodel @html.checkboxfor(m => m.selected) @html.labelfor(m => m.selected, model.rolename)
accountcontroller.cs
[allowanonymous] public actionresult register() { var roles = roles.getallroles(); var model = new registerviewmodel { roles = roles.select(role => new roleviewmodel { rolename = role, selected = false }) }; return view(model); }
i'm missing obvious, can't seem see it. appreciated.
probably problem there no html-input
rolename
in roleeditor
. example can add hidden input.
@model simplemembershiptest.models.roleviewmodel @html.checkboxfor(m => m.selected) @html.labelfor(m => m.selected, model.rolename) @html.hiddenfor(m => m.rolename)
also article may useful http://dotnetslackers.com/articles/aspnet/understanding-asp-net-mvc-model-binding.aspx
Comments
Post a Comment