Boo
Boo

Reputation: 1644

ASP.NET MVC3 Html.EditorFor() problem

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"

Upvotes: 0

Views: 19805

Answers (4)

DKR
DKR

Reputation: 5734

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

Upvotes: 0

user4296392
user4296392

Reputation: 21

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" } })

Upvotes: 2

Boo
Boo

Reputation: 1644

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?

Upvotes: 0

Buildstarted
Buildstarted

Reputation: 26689

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.

Upvotes: 5

Related Questions