MVC Radiobutton绑定复杂对象

时间:2011-07-10 03:20:41

标签: asp.net-mvc asp.net-mvc-3 radio-button

我有MVC3网络应用程序,我们需要通过验证填充单选按钮列表。我的模型是这样的:

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

我需要填写如下所示的radiobutton列表:

  • Employee1ID - Employee1Name - Employee1Department // id - name - department
  • Employee2ID - Employee2Name - Employee2Department
  • Employee3ID - Employee3Name - Employee3Department

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

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

非常感谢你的帮助!

谢谢,

1 个答案:

答案 0 :(得分:11)

这是我推荐你的。从一个干净的视图模型开始,该模型真正表达视图包含的信息:

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; }
}

然后是控制器:

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" />
}