asp.net-mvc – MVC Radiobutton绑定复杂对象

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – MVC Radiobutton绑定复杂对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有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}
 }

我需要填充radiobutton列表,如下所示:

> Employee1ID – Employee1Name – Employee1Department // id – name – department
> Employee2ID – Employee2Name – Employee2Department
> Employee3ID – Employee3Name – Employee3Department

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

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

非常感谢您的帮助!

谢谢,

解决方法

这是我建议你的.从一个干净的视图模型开始,一个真正表达视图包含信息的视图:
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" />
}
原文链接:https://www.f2er.com/aspnet/251240.html

猜你在找的asp.Net相关文章