开发者

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="Use开发者_如何学运维rName_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"


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.


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


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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜