Working with multiple html helpers with the same name in mvc4
I have several html helpers. When I click the login button, I disable the user_register div using jquery, and when I enter the data, username and password that the binding device can bind properly, but when I click Register, I disable the user_login div and allow user_register div and when I only enter email and firstname data is what the model binder can bind, not username, password. This is becoz I use the same html helpers more than once. How to deal with this. I have the following code
<div class="user_login">
<label>Email / Username</label>
@Html.TextBoxFor(model => model.Email)
<br />
<label>Password</label>
@Html.TextBoxFor(model => model.Password)
<br />
<div class="action_btns">
<div class="one_half"><a href="#" class="btn back_btn"><i class="fa fa-angle-double-left"></i>Back</a>
</div>
<div class="one_half last">
<input type="submit" style="border: none" name="action" class="btn btn_red" value="Login" />
</div>
</div>
<a href="#" class="forgot_password">Forgot password?</a>
</div>
<div class="user_register">
<label>Full Name</label>
@Html.TextBoxFor(model => model.First_Name)<br />
<label>Email Address</label>
@Html.TextBox(model=>model.Email)<br />
<label>User Name</label>
@Html.TextBoxFor(model => model.User_Name)<br />
<label>Password</label>
@Html.TextBox(model=>model.Password") <br />
</div>
The controller follows here
public ActionResult Index(Customer customer, string Action)
{
//something here
}
You didn't show the full view, but assuming all of these controls are generated inside a single form element, then all controls will post back unless you disable inputs. If so, then the first input is name="Email"
bound and the second is ignored DefaultModelBinder
. (since the first one is only hidden, its value is probably empty). Your script should ensure that 2 inputs in the login section are blocked, like
@Html.TextBoxFor(model => model.Email, new { id = "login-email" })
and
$('#login-email').prop('disabled', true);
Pay attention to the attribute id
so you can choose the correct one (and prevent duplicate id, which is invalid html.
An alternative would be to create two separate forms.