asp.net MVC中枚举的Html helper



我有两个模型类:

  1. 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
    }
    

    EmployeeCreate视图中,我想显示有可能检查其中几个的项目。

    我已经创建了一个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创建foreach循环。CheckBoxFor,我试着在这个指令中这样做->http://findnerd.com/list/view/How-to-bind-checkbox-with-enum-values-in-MVC/25707/

    但它并没有真正起作用:我没有向projectttypes .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循环枚举?

    我已经看到错误了- "未定义也未赋值。你是怎么编译这个的?

    @Html.LabelFor(m => m.CheckBoxProjectType[i].ProjectType);
    

    用户只是"项目";相反。

    我在你的create view和action中也没有看到@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(我认为你必须添加一些雇员输入控件太):

    
    @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>
    

    相关内容

    • 没有找到相关文章

    最新更新