我有两个模型类:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ProjectType Project { get; set; }
}
public enum ProjectType
{
Project1,
Project2,
Project3,
Project4
}
在的Create
视图中Employee
,我希望显示可以检查其中几个项目的项目。
我已经在“创建”视图中创建了一个foreach
循环Html.RadioButtonFor
@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 -MVC / 25707中的-enum-values /
但这并没有真正起作用:我没有在ProjectType.cs中添加任何内容。我在Employee.cs中添加了一个类
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();
}
在创建视图中,我试图从之前发布的链接中复制循环
@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);
用户只需“ Projects”即可。
而且我在您的create视图和acton中也看不到任何@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);
}
在您的创建视图中,用EmployeeModel替换Employee(我认为您也必须添加一些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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句