加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 00:00:19 所属栏目:asp.Net 来源:网络整理
导读:我有MVC3 Web应用程序,我们需要通过验证填充单选按钮列表.我的模型是这样的: public class EmployeesViewModel{ public ListEmployee listEmployee { get; set; } //To persist during post public IEnumerableSelectListItem selectListEmployee { get; se
我有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" />
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读