使用ApplicationUser

时间:2016-03-23 21:29:35

标签: razor asp.net-mvc-5

我正在使用Identity 2.0,在我的ApplicationUser中,我有以下内容:

ApplicationUser.cs

    public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        //other things omitted for brevity
    }

在我看来,我有以下简化版本:

@model ApplicationUser
@using (Html.BeginForm("Details", "Admin", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
<dl class="dl-horizontal ">
    <dt>
        @Html.DisplayNameFor(model => model.FirstName)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.FirstName)

    </dd>

    <dt>
        @Html.DisplayNameFor(model => model.LastName)
    </dt>

    <dd>
        @Html.DisplayFor(model => model.LastName)
    </dd>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
}

我的控制器

        [HttpPost]
        public async Task<ActionResult> Details (ApplicationUser model)
        {
            //do checks on which items have been selected
            return View();
        }

我想在每个标签旁边添加复选框,在我的POST操作中,我希望能够看到哪些已被选中。例如。 FirstName = checked,LastName = notChecked或类似的东西。 我应该采取哪种方法?我试过了checkboxfor,但没有成功。

1 个答案:

答案 0 :(得分:1)

我认为最好使用ViewModel:

public class UserViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool UseFirstName { get; set; }
    public bool UseLastName { get; set; }
}

然后在get方法和视图中将IdentityUser映射到UserViewModel:

@model UserViewModel
@using (Html.BeginForm("Details", "Admin", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{  
<dl class="dl-horizontal ">
 <dt>
     @Html.DisplayNameFor(model => model.FirstName)
 </dt>

 <dd>
 @Html.CheckBoxFor(model => model.UseFirstName)
 @Html.DisplayFor(model => model.FirstName)

</dd>