Html Asp .Net MVC 中的枚举助手
Html helpers for enum in Asp .Net MVC
我有两个型号classes:
- Employee.cs
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ProjectType Project { get; set; }
}
- ProjectType.cs
public enum ProjectType
{
Project1,
Project2,
Project3,
Project4
}
在 Employee
的 Create
视图中,我想显示可以检查其中几个项目的项目。
我在创建视图
中用Html.RadioButtonFor
创建了一个foreach
循环
@model App.Data.Models.Employee
@using App.Data.Models;
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
}
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Project, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@foreach (var project in Enum.GetValues(typeof(ProjectType)))
{
<div class="col-md-10">
@Html.RadioButtonFor(m => m.Project, project)
@Html.Label(project.ToString())
</div>
}
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
如果我能够选择多个选项,那会很好用。我假设在这种情况下,我应该使用 Html.CheckBoxFor 创建 foreach 循环,并且我试图像在这条指令中那样做 -> http://findnerd.com/list/view/How-to-bind-checkbox-with-enum-values-in-MVC/25707/
但它并没有真正起作用:
我没有向 ProjectType.cs 添加任何内容。我在 Employee.cs
中添加了一个 class
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<EnumModel> CheckBoxProjectType { get; set; }
}
public class EnumModel
{
public ProjectType ProjectType { get; set; }
public bool isSelected { get; set; }
}
当我在 EmployeeController 中创建循环时 - CheckBoxProjectType 带有红色下划线:
public ActionResult Create()
{
ProjectType model = new ProjectType();
model.CheckBoxProjectType = new List<EnumModel>();
foreach (ProjectType projectType in Enum.GetValues(typeof(ProjectType)))
{
model.CheckBoxProjectType.Add(new EnumModel() { ProjectType = projectType, isSelected = false });
}
return View();
}
在创建视图中,我试图从我之前发布的 link 中复制循环
@model App.Data.Models.Employee
@using App.Data.Models;
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
}
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-10">
@for (int i = 0; i < Model.CheckBoxProjectType.Count; i++)
{
@Html.DisplayFor(m => m.CheckBoxProjectType[i].ProjectType);
@Html.CheckBoxFor(m => m.CheckBoxProjectType[i].isSelected);
@Html.HiddenFor(m => m.CheckBoxProjectType[i].ProjectType);
}
@Html.ValidationMessageFor(model => model.ProjectType, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
谁能指导我如何为枚举创建 foreach 循环?
我已经可以看到错误 - “i”未定义且未分配。你怎么能编译这个?
@Html.LabelFor(m => m.CheckBoxProjectType[i].ProjectType);
用户只是“项目”。
而且我在您的创建视图和操作中也没有看到任何@model。
public ActionResult Create()
{
ProjectType model = new ProjectType();
//Where is your ProjectType class?
// and why I see an Employee AS a model in your view?
......
//you have to change your return to this
return View(model);
}
最好创建一个像这样的特殊 viewModel 用作模型(将其放在单独的文件中):
public class EmployeeViewModel
{
public Employee Employee{ get; set; }
public List<EnumModel> EnumModels{ get; set; }
}
您的员工将是:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<ProjectType > ProjectTypes { get; set; }
}
您的新创建操作将如下所示:
public ActionResult Create()
{
var model = new EmployeeViewModel{
Employee=new Employee,
EnumModels=new List<EnumModel>()
};
foreach (var projectType in Enum.GetValues(typeof(ProjectType)))
{
model.EnumModels.Add(new EnumModel { ProjectType = projectType,
isSelected = false });
}
return View(model);
}
并在您的创建视图中将 Employee 替换为 EmployeeModel(我认为您还必须添加一些 Employee 输入控件):
@using App.Data.Models
@model EmployeeViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
}
<div class="form-horizontal">
<div class="form-group">
<h5> Projects </h5>
<div class="col-md-10">
@for (int i = 0; i < Model.EnumModels.Count; i++)
{
@Html.DisplayFor(m => m.EnumModels[i].ProjectType);
@Html.CheckBoxFor(m => m.EnumModels[i].isSelected);
@Html.HiddenFor(m => m.EnumModels[i].ProjectType);
}
@Html.ValidationMessageFor(model => model.EnumModels, "", new {
class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
使用此代码:
public ActionResult Create()
{
Employee model = new Employee();
model.CheckBoxProjectType = new List<EnumModel>();
foreach (ProjectType projectType in Enum.GetValues(typeof(ProjectType)))
{
model.CheckBoxProjectType.Add(new EnumModel() { ProjectType = projectType, isSelected = false });
}
return View(model);
}
并查看:
@using App.Data.Models.Employee;
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
}
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-10">
@for (int i = 0; i < Model.CheckBoxProjectType.Count; i++)
{
@Html.DisplayFor(m => m.CheckBoxProjectType[i].ProjectType);
@Html.CheckBoxFor(m => m.CheckBoxProjectType[i].isSelected);
@Html.HiddenFor(m => m.CheckBoxProjectType[i].ProjectType);
}
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
我有两个型号classes:
- Employee.cs
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ProjectType Project { get; set; }
}
- ProjectType.cs
public enum ProjectType
{
Project1,
Project2,
Project3,
Project4
}
在 Employee
的 Create
视图中,我想显示可以检查其中几个项目的项目。
我在创建视图
中用Html.RadioButtonFor
创建了一个foreach
循环
@model App.Data.Models.Employee
@using App.Data.Models;
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
}
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Project, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@foreach (var project in Enum.GetValues(typeof(ProjectType)))
{
<div class="col-md-10">
@Html.RadioButtonFor(m => m.Project, project)
@Html.Label(project.ToString())
</div>
}
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
如果我能够选择多个选项,那会很好用。我假设在这种情况下,我应该使用 Html.CheckBoxFor 创建 foreach 循环,并且我试图像在这条指令中那样做 -> http://findnerd.com/list/view/How-to-bind-checkbox-with-enum-values-in-MVC/25707/
但它并没有真正起作用: 我没有向 ProjectType.cs 添加任何内容。我在 Employee.cs
中添加了一个 class public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<EnumModel> CheckBoxProjectType { get; set; }
}
public class EnumModel
{
public ProjectType ProjectType { get; set; }
public bool isSelected { get; set; }
}
当我在 EmployeeController 中创建循环时 - CheckBoxProjectType 带有红色下划线:
public ActionResult Create()
{
ProjectType model = new ProjectType();
model.CheckBoxProjectType = new List<EnumModel>();
foreach (ProjectType projectType in Enum.GetValues(typeof(ProjectType)))
{
model.CheckBoxProjectType.Add(new EnumModel() { ProjectType = projectType, isSelected = false });
}
return View();
}
在创建视图中,我试图从我之前发布的 link 中复制循环
@model App.Data.Models.Employee
@using App.Data.Models;
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
}
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-10">
@for (int i = 0; i < Model.CheckBoxProjectType.Count; i++)
{
@Html.DisplayFor(m => m.CheckBoxProjectType[i].ProjectType);
@Html.CheckBoxFor(m => m.CheckBoxProjectType[i].isSelected);
@Html.HiddenFor(m => m.CheckBoxProjectType[i].ProjectType);
}
@Html.ValidationMessageFor(model => model.ProjectType, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
谁能指导我如何为枚举创建 foreach 循环?
我已经可以看到错误 - “i”未定义且未分配。你怎么能编译这个?
@Html.LabelFor(m => m.CheckBoxProjectType[i].ProjectType);
用户只是“项目”。
而且我在您的创建视图和操作中也没有看到任何@model。
public ActionResult Create()
{
ProjectType model = new ProjectType();
//Where is your ProjectType class?
// and why I see an Employee AS a model in your view?
......
//you have to change your return to this
return View(model);
}
最好创建一个像这样的特殊 viewModel 用作模型(将其放在单独的文件中):
public class EmployeeViewModel
{
public Employee Employee{ get; set; }
public List<EnumModel> EnumModels{ get; set; }
}
您的员工将是:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<ProjectType > ProjectTypes { get; set; }
}
您的新创建操作将如下所示:
public ActionResult Create()
{
var model = new EmployeeViewModel{
Employee=new Employee,
EnumModels=new List<EnumModel>()
};
foreach (var projectType in Enum.GetValues(typeof(ProjectType)))
{
model.EnumModels.Add(new EnumModel { ProjectType = projectType,
isSelected = false });
}
return View(model);
}
并在您的创建视图中将 Employee 替换为 EmployeeModel(我认为您还必须添加一些 Employee 输入控件):
@using App.Data.Models
@model EmployeeViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
}
<div class="form-horizontal">
<div class="form-group">
<h5> Projects </h5>
<div class="col-md-10">
@for (int i = 0; i < Model.EnumModels.Count; i++)
{
@Html.DisplayFor(m => m.EnumModels[i].ProjectType);
@Html.CheckBoxFor(m => m.EnumModels[i].isSelected);
@Html.HiddenFor(m => m.EnumModels[i].ProjectType);
}
@Html.ValidationMessageFor(model => model.EnumModels, "", new {
class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
使用此代码:
public ActionResult Create()
{
Employee model = new Employee();
model.CheckBoxProjectType = new List<EnumModel>();
foreach (ProjectType projectType in Enum.GetValues(typeof(ProjectType)))
{
model.CheckBoxProjectType.Add(new EnumModel() { ProjectType = projectType, isSelected = false });
}
return View(model);
}
并查看:
@using App.Data.Models.Employee;
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
}
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-10">
@for (int i = 0; i < Model.CheckBoxProjectType.Count; i++)
{
@Html.DisplayFor(m => m.CheckBoxProjectType[i].ProjectType);
@Html.CheckBoxFor(m => m.CheckBoxProjectType[i].isSelected);
@Html.HiddenFor(m => m.CheckBoxProjectType[i].ProjectType);
}
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>