Asp.NET MVC Binding - Using the Binding Attribute Attribute to Assign a Prefix for a Simple Type

I have a .net mvc application with a controller action that accepts user registration entries. I have the following user interface fields: emailaddress, firstname, lastname, password and confirmpassword. Some of these fields are not related to the model object (i.e., Password confirmation does not apply to the user model, only the password). My registration form is on the same view as the login form. Thus, I have independent forms on the same point of view, each of which returns to different actions.

I realized that I can assign prefixes to form elements to separate such fields between register and login. The problem was in checking, if an error occurred, the view was reloaded, a verification message was displayed, but fields such as e-mail (which exist in the login and register) will be filled with the previously entered address. In addition, I had a validation summary above the login and registration fields. When an error occurred during registration, error messages were filled in both test results. I decided to assign prefixes for the fields (register.fieldname and login.fieldname), which can help in these problems.

So, when the register action processes the message, it no longer finds the values ​​for the fields of the registration form and instead returns null. The following is the method signature used for this action ...

Any contribution to what is happening here will be great.

Thanks Jeremy

public ActionResult Register([Bind(Prefix = "Register")] string emailAddress, [Bind(Prefix = "Register")] string firstName, [Bind(Prefix = "Register")] string LastName, [Bind(Prefix = "Register")] string password, [Bind(Prefix = "Register")] string confirmPassword) 

and the following from my ui, it presents the registration form ....

 <h2>Create a New Account</h2> <p> Use the form below to create a new account. </p> <p> Passwords are required to be a minimum of <%=Html.Encode(ViewData["PasswordLength"])%> characters in length. </p> <%= Html.ValidationSummary() %> <% using (Html.BeginForm("register", "account")) { %> <div> <fieldset> <legend>Account Information</legend> <table> <tr> <td><label for="EmailAddress">Email Address:</label></td> <td> <%= Html.TextBox("Register.EmailAddress") %> <%= Html.ValidationMessage("Register.EmailAddress")%> </td> </tr> <tr> <td><label for="FirstName">First Name</label></td> <td> <%= Html.TextBox("Register.FirstName")%> <%= Html.ValidationMessage("Register.FirstName")%> </td> </tr> <tr> <td><label for="LastName">Last Name</label></td> <td> <%= Html.TextBox("Register.LastName")%> <%= Html.ValidationMessage("Register.LastName")%> </td> </tr> <tr> <td><label for="password">Password:</label></td> <td> <%= Html.Password("Register.password")%> <%= Html.ValidationMessage("Register.password")%> </td> </tr> <tr> <td><label for="confirmPassword">Confirm password:</label></td> <td> <%= Html.Password("Register.confirmPassword")%> <%= Html.ValidationMessage("Register.confirmPassword")%> </td> </tr> <tr> <td colspan="2" class="alignright"> <input type="submit" value="Register" /> </td> </tr> </table> </fieldset> </div> <% } %> </div> 
+4
source share
2 answers

I think in order to use [Bind] with a prefix, you need to use a complex type. You associate the same prefix with each parameter that does not work with how MVC does it.

You can create a type for processing a form message:

 public class RegistrationForm { string EmailAddress {get;set;} string FirstName {get;set;} string LastName {get;set;} string Password {get;set;} string ConfirmPassword {get;set;} } 

Then you can change your signature to:

 public ActionResult Register(RegistrationForm register) { ... } 
+6
source

It seems that for simple types such as int and string, the Bind prefix overrides the model name for the parameters. Using an example action method, DefaultModelBinder will look for the name "Register" for all parameters.

To get around this and not require a complex type, you need to specify the full name, since Mvc will display it in the HTML markup.

Applying this to your action method, it should look like this:

 public ActionResult Register([Bind(Prefix = "Register.EmailAddress")] string emailAddress, [Bind(Prefix = "Register.FirstName")] string firstName, [Bind(Prefix = "Register.LastName")] string LastName, [Bind(Prefix = "Register.Password")] string password, [Bind(Prefix = "Register.onfirmPassword")] string confirmPassword) 
0
source

All Articles