0

i try to bind data to model with Html.EditorFor() helper and submit, but model come to controller is null.

Here is code of model:

public class LogOnModel
{
    [LocalizedRequired]
    [LocalizedDisplayName("User Name")]
    public string UserName { get; set; }

    [LocalizedRequired]
    [DataType(DataType.Password)]
    [LocalizedDisplayName("Password")]
    public string Password { get; set; }

    [LocalizedDisplayName("Remember Me")]
    public bool RememberMe { get; set; }
}

this is cshtml:

@model Models.LogOnModel
{
    View.Title = "Log On";
}
@using (Html.BeginForm())
{
    @Html.EditorFor(m => m.UserName);
    @Html.EditorFor(m => m.Password);
    <input type="submit" value="LogOn" />
}

and html code is generate like this:

<input id="UserName_UserName" name="UserName.UserName" type="text" value="qwerty" />
<input id="Password_Password" name="Password.Password" type="password" />

it seems like error in html-generated code, it should be id="someid" value="somevalue", but not id="someid_someid" value="somevalue.somevalue"

2
  • 1
    Did you create your own Editor template? If so, could you post that code? Commented Dec 1, 2010 at 14:02
  • [DataType(DataType.Password)] with EditorFor worked fine for me. My id and name attributes were just "Password" like I expected. Commented Feb 18, 2016 at 21:49

4 Answers 4

5

Since you're just using textboxes you could always use the following

@using (Html.BeginForm()) {
    @Html.TextBoxFor(m => m.UserName);
    @Html.PasswordFor(m => m.Password);
   <input type="submit" value="LogOn" />
}

Otherwise it might depend on custom templates that you've created.

Sign up to request clarification or add additional context in comments.

Comments

2

Hi You Can Solve it by just putting @type directive just like this:

 @Html.EditorFor(model => model.strPassword, new { htmlAttributes = new { @class = "form-control",@type="password" } })

Comments

0

thanks! yes, it was problem in editor template:

<div class="textinput">@Html.TextBox(ViewData.ModelMetadata.PropertyName, Model)</div>

i solve it like this:

<div class="textinput">@Html.TextBox(String.Empty)</div>

is it a good solution?

3 Comments

<div class="textinput">@Html.TextBoxFor(m => m)</div> should work for you.
yes, it works! thanks! can you explain me difference between my solution and yours?
Why do you need an editor template for a simple string field? And, why do you feel necessary to use LocalizedRequired and LocalizedDisplayName instead of Required and DisplayName?
0
@using (Html.BeginForm()) {
    @Html.TextBoxFor(m => m.UserName);
    @Html.TextBoxFor(m => m.Password);
   <input type="submit" value="LogOn" />
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.