MVC Radiobutton 绑定复杂对象

Posted

技术标签:

【中文标题】MVC Radiobutton 绑定复杂对象【英文标题】:MVC Radiobutton binding complex object 【发布时间】:2011-10-02 02:38:54 【问题描述】:

我有 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. 将“Employee”对象标记为必填字段的最佳方法

非常感谢您的帮助!

谢谢,

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

谢谢达林!该视图模型确实可以达到目的! 期待您的更多想法。假设我的“员工”表中有复合键怎么办? (就像“ID”和“部门”一起形成复合键)在这种情况下,我需要传递 ID 和部门来获取我想要的真实对象。最好的方法是什么? @matmat,单选按钮只发送一个值,因此在这种情况下使用复合键没有意义。您可以使用数据库的唯一主键。作为替代方案,您可以将 ID 属性设置为ID_DEPT 形式的字符串,当表单被回发并获取两个值时,您可以在控制器中拆分它并获取两个值,但这似乎很骇人听闻。唯一的主键是一种更好的方法。

以上是关于MVC Radiobutton 绑定复杂对象的主要内容,如果未能解决你的问题,请参考以下文章

DataGrid 创建 RadioButton 列

复杂抽象对象的WebAPI自定义模型绑定

wpf中datagrid选择改变事件中怎样绑定RadioButton的数据

MVC复杂类型的模型绑定

radioButton& listBox&numericUpDown 操作

鸿蒙App开发(11)---RadioButton与RadioContainer组件