MVC单选框结合复合物 [英] MVC Radiobutton binding complex object

查看:90
本文介绍了MVC单选框结合复合物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have MVC3 web application where we need to populate radio button list with validation. My Model is something like this:

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

我需要填充单选按钮列表类似如下:

I need to populate radiobutton list something like below:


  • Employee1ID - Employee1Name - Employee1Department // ID - 姓名 - 部门

  • Employee2ID - Employee2Name - Employee2Department

  • Employee3ID - Employee3Name - Employee3Department

选定员工应该被存储到selectedEmployee字段。什么是填充MVC3这些单选按钮列表中的最佳或干净的方式是什么?

Selected Employee should be stored into "selectedEmployee" field. What is the best or clean way to populate these radio button List in MVC3?

注:主要是寻找两个工作:
 1.存储在每个输入单选按钮标签雇员对象,这样选定员工将被保存到selectedEmployee字段
 2.最好的方式来纪念雇员对象所需的字段

Note: Mainly Looking for two task: 1. storing "Employee" object in each "Input" radio button tag, so that selected employee will be saved to "selectedEmployee" field 2. Best way to mark "Employee" object as required field

大部分AP preciate您的帮助!

Much appreciate your help!

谢谢,

推荐答案

下面是我会推荐你​​。开始用干净的视图模型,一个真正的前presses什么视图包含的信息:

Here's what I would recommend you. Start with a clean view model, one that really expresses what the view contains as information:

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

这篇关于MVC单选框结合复合物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆