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 选定的员工应存储在“selectedEmployee”字段中.在MVC3中填充这些单选按钮列表的最佳或最简洁的方法是什么? 注意:主要寻找两个任务: 非常感谢您的帮助! 谢谢, 解决方法
这是我建议你的.从一个干净的视图模型开始,一个真正表达视图包含信息的视图:
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" /> } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 注册到global.asax的路由的替代位置
- asp.net – 为什么我在StateServer模式下运行时会丢失会话?
- ASP.NET MVC3中具有多个类的ActionLink
- asp.net-mvc-3 – 增加ASP.NET MVC 3中的超时值
- asp.net – 可以通过移动设备的Web浏览器上传图片吗?
- ASP.NET MVC用户 – 您是否想念WebForms中的任何内容?
- asp.net-mvc – 如何从HttpContext访问ViewData? (ASP.Net
- ASP.NET验证摘要:如何使用JavaScript禁用验证程序?
- ASP.net服务器事件处理客户端
- 给微软的日志框架写一个基于委托的日志提供者
推荐文章
站长推荐
- asp.net-mvc-4 – MVC4 StyleBundle不以正确的顺
- asp.net-mvc – MVC .net属性上必需属性的布尔值
- asp.net-mvc – 对Mono XSP托管的Web应用程序的第
- Asp.Net Mvc表单提交之List集合
- asp.net – 为什么在设置@Page masterPageFIle时
- 如何在ASP.NET表中创建thead和tbody?
- asp.net-web-api – WebAPI:403在发布网站后被禁
- asp.net-mvc – 使用路由操作URL
- asp.net-mvc – MVC模型中的数据库查询
- asp.net-mvc – 具有插件和多租户支持的ASP.NET
热点阅读