gpt4 book ai didi

asp.net-mvc - MVC Radiobutton 绑定(bind)复杂对象

转载 作者:行者123 更新时间:2023-12-04 17:12:29 24 4
gpt4 key购买 nike

我有 MVC3 Web 应用程序,我们需要在其中使用验证填充单选按钮列表。我的模型是这样的:

public class EmployeesViewModel
{
public List<Employee> listEmployee { get; set; } //To persist during post
public IEnumerable<SelectListItem> selectListEmployee { get; set; }

[Required]
public Employee selectedEmployee { get; set; }
}

public class Employee
{
public int ID {get; set;}
public string Name {get; set}
public string Department {get; set}
}

我需要填充单选按钮列表,如下所示:
  • Employee1ID - Employee1Name - Employee1Department//id - 名称 - 部门
  • Employee2ID - Employee2Name - Employee2Department
  • Employee3ID - Employee3Name - Employee3Department

  • 选定的员工应存储在“selectedEmployee”字段中。在 MVC3 中填充这些单选按钮列表的最佳或干净的方法是什么?

    注意:主要找两个任务:
    1. 在每个“Input”单选按钮标签中存储“Employee”对象,以便将选中的员工保存到“selectedEmployee”字段中
    2.将“员工”对象标记为必填字段的最佳方法

    非常感谢您的帮助!

    谢谢,

    最佳答案

    这是我要推荐给你的。从一个干净的 View 模型开始,它真正将 View 包含的内容表达为信息:

    public class EmployeesViewModel
    {
    public List<EmployeeViewModel> ListEmployee { get; set; }

    [Required]
    public int? SelectedEmployeeId { get; set; }
    }

    public class EmployeeViewModel
    {
    public int ID { get; set; }
    public string Label { get; set; }
    }

    然后是 Controller :
    public class HomeController : Controller
    {
    public ActionResult Index()
    {
    var model = new EmployeesViewModel
    {
    ListEmployee = GetEmployees()
    };
    return View(model);
    }

    [HttpPost]
    public ActionResult Index(EmployeesViewModel model)
    {
    if (!ModelState.IsValid)
    {
    // the model is invalid, the user didn't select an employee
    // => refetch the employee list from the repository and
    // redisplay the view so that he can fix the errors
    model.ListEmployee = GetEmployees();
    return View(model);
    }

    // validation passed at this stage
    // TODO: model.SelectedEmployeeId will contain the id
    // of the selected employee => use your repository to fetch the
    // actual employee object and do something with it
    // (like grant him the employee of the month prize :-))

    return Content("thanks for submitting", "text/plain");
    }

    // TODO: This doesn't belong here obviously
    // it's only for demonstration purposes. In the real
    // application you have a repository, use DI, ...
    private List<EmployeeViewModel> GetEmployees()
    {
    return new[]
    {
    new EmployeeViewModel { ID = 1, Label = "John (HR)" },
    new EmployeeViewModel { ID = 2, Label = "Peter (IT)" },
    new EmployeeViewModel { ID = 3, Label = "Nathalie (Sales)" },
    }.ToList();
    }
    }

    最后是一个观点:
    @model EmployeesViewModel

    @using (Html.BeginForm())
    {
    @Html.ValidationMessageFor(x => x.SelectedEmployeeId)
    @foreach (var employee in Model.ListEmployee)
    {
    <div>
    @Html.RadioButtonFor(x => x.SelectedEmployeeId, employee.ID, new { id = "emp" + employee.ID })
    @Html.Label("emp" + employee.ID, employee.Label)
    </div>
    }
    <input type="submit" value="OK" />
    }

    关于asp.net-mvc - MVC Radiobutton 绑定(bind)复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6638966/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com