Razor 使用ApplicationUser从视图中选中的项目
我使用的是Identity 2.0,在我的应用程序用户中,我有以下内容: ApplicationUser.csRazor 使用ApplicationUser从视图中选中的项目,razor,asp.net-mvc-5,Razor,Asp.net Mvc 5,我使用的是Identity 2.0,在我的应用程序用户中,我有以下内容: ApplicationUser.cs public class ApplicationUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } //other things omitted for brevity }
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>
}
@model应用程序用户
@使用(Html.BeginForm(“Details”,“Admin”,FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@DisplayNameFor(model=>model.FirstName)
@DisplayFor(model=>model.FirstName)
@DisplayNameFor(model=>model.LastName)
@DisplayFor(model=>model.LastName)
}
我的控制器
[HttpPost]
public async Task<ActionResult> Details (ApplicationUser model)
{
//do checks on which items have been selected
return View();
}
[HttpPost]
公共异步任务详细信息(ApplicationUser模型)
{
//检查已选择的项目
返回视图();
}
我想在每个标签旁边添加复选框,在我的POST操作中,我想能够看到哪些标签被选中。例如,FirstName=选中,LastName=刻痕或任何类似的东西。
我应该采取哪种方法?我尝试了checkboxfor,但没有成功。我认为最好使用ViewModel:
public class UserViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool UseFirstName { get; set; }
public bool UseLastName { get; set; }
}
然后将IdentityUser映射到get方法和视图中的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>
@model UserViewModel
@使用(Html.BeginForm(“Details”,“Admin”,FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@DisplayNameFor(model=>model.FirstName)
@Html.CheckBoxFor(model=>model.UseFirstName)
@DisplayFor(model=>model.FirstName)
这就是为什么我们使用您想要实现的目标?复选框的意义是什么?如果您想要一些复选框,请添加属性bool IsFirstNameChecked
等并添加@Html.CheckBoxFor(m=>m.IsFirstNameChecked)
在您的视图中。我将使用所选项目向未正确填充字段的用户发送电子邮件。出于简单的原因,这里提供了名字和姓氏。@Jasen,这是正确的,我知道视图模型,因为我在其他地方使用它。但在这里,我特别希望能够重用ApplicationUser类,而不必使用必须手动添加已经存在的属性。比如说20个属性,每个属性都需要一个bool变量。维护起来很麻烦。是否有其他方法可以包装它而不使用zillion bool变量?表单post按名称绑定。因为您不想只向任何模型添加任何属性。那么您的操作将包括额外的参数eters详细信息(ApplicationUser model,bool firstNameChecked,bool lastNameChecked)
。一种说法是,但这意味着我必须更新我的ApplicationUser和这个,可能还有其他viewModels。鉴于我有20个属性,维护起来会很麻烦,因为在VM的实现中我会有40个属性。