asp.net-mvc – MVC Radiobutton绑定复杂对象
发布时间:2020-05-23 22:49:14 所属栏目:asp.Net 来源:互联网
导读:我有MVC3 Web应用程序,我们需要通过验证填充单选按钮列表.我的模型是这样的: public class EmployeesViewModel{ public ListEmployee listEmployee { get; set; } //To persist during post public IEnumerableSelectListItem selec
|
我有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 – 什么叫Page_Load,它是如何做的?
- asp.net-mvc – HTML5元素的“传说”太少了
- asp.net-mvc – 类型或命名空间名称“Mvc”不存在
- asp.net-mvc-3 – 后退按钮或导航到特定视图(页面)-ASP.NET
- ASP.NET Forms Authentication阻止在Login.aspx上加载javas
- asp.net-mvc-3 – 在Application_Start中访问ninject内核
- asp.net-mvc – 奇怪的MVC问题
- asp.net-mvc – 将id类型从string更改为int时,如何在Web AP
- .net – SignalR – connection.hubName未定义
- ASP.net vs PHP(选择什么)
推荐文章
站长推荐
- asp.net-mvc – 在ASP.NET MVC中对ViewModels进行
- asp.net-mvc-3 – 超时在ASP.Net MVC FormsAuthe
- asp.net-mvc-3 – ModelState.AddModelError不显
- asp-classic – HTTP / 1.1新应用程序失败
- 在Asp.net mvc5中使用用户名而不是电子邮件身份
- asp.net-mvc – 使用像Stackoverflow这样的查询字
- asp.net – Web部署安装程序(MSI)中没有“IIS 7部
- asp.net-mvc – 怎么样?控制器返回任何/当前视图
- ASP.NET(MVC2)中%:%做了什么?
- 身份更改GUID为int
热点阅读
