asp.net-mvc-3 – 如何将单选按钮与ASP.Net MVC中的模型数据进行绑定?
发布时间:2020-05-24 22:59:02 所属栏目:asp.Net 来源:互联网
导读:我想以我的形式显示一个将从模型数据填充的单选按钮. 这是我的模特儿 public class Student { [Required(ErrorMessage = First Name Required)] // textboxes will show [Display(Name = First Name :)] [StringLen
|
我想以我的形式显示一个将从模型数据填充的单选按钮. 这是我的模特儿 public class Student
{
[Required(ErrorMessage = "First Name Required")] // textboxes will show
[Display(Name = "First Name :")]
[StringLength(5,ErrorMessage = "First Name cannot be longer than 5 characters.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name Required")] // textboxes will show
[Display(Name = "Last Name :")]
[StringLength(5,ErrorMessage = "Last Name cannot be longer than 5 characters.")]
public string LastName { get; set; }
[Required(ErrorMessage = "Sex Required")] // group of radio button will show
[Display(Name = "Sex :")]
public List<Sex> Sex { get; set; }
}
public class Sex
{
public string ID { get; set; }
public string Type { get; set; }
}
在这里,我正在尝试从动作方法手动填充学生模型 public AcitonResult Index()
{
var student= new Student
{
FirstName = "Rion",LastName = "Gomes",Sex= new List<Sex>
{
new Sex{ID="1",Type = "Male"},new Sex{ID="2",Type = "Female"}
}
}
return View(student);
}
现在我怎么可以生成一个单选按钮,它将以Male&女性和值将具有ID 我搜索了谷歌,发现很多样本,我使用了一个但不确定它是否可行. @Html.RadioButtonFor(x => x.Sex,"Male") 我不想硬编码男性或女性;我想通过一个模型显示它,并希望在for循环中生成单选按钮. 我也是新MVC,所以请指导我. 解决方法如果我理解正确,您应该更改您的学生模型,以使属性“性别”为一个整数,然后您应该有另一个属性称为“SexList”,您填充列表.此更改将允许您发布数据并检索用户选择的性别.如果你使用Razor视图引擎,你应该这样做: @{
foreach (var sex in Model.SexList)
{
<div>
@Html.RadioButtonFor(model => model.Sex,new { id = "sex" + sex.ID })
@Html.Label("sex" + sex.ID,sex.Type)
</div>
}
}
编辑: 你的模型应该是这样的: public class Student
{
[Required(ErrorMessage = "First Name Required")] // textboxes will show
[Display(Name = "First Name :")]
[StringLength(5,ErrorMessage = "First Name cannot be longer than 5 characters.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name Required")] // textboxes will show
[Display(Name = "Last Name :")]
[StringLength(5,ErrorMessage = "Last Name cannot be longer than 5 characters.")]
public string LastName { get; set; }
[Required(ErrorMessage = "Sex Required")]
[Display(Name = "Sex :")]
public Sex Gender { get; set; }
public List<Sex> SexList { get; set; }
}
public class Sex
{
public string ID {get;set;}
public string Type {get;set;}
}
您在控制器中的操作: [HttpGet]
public AcitonResult Index()
{
var student = new Student
{
FirstName = "Rion",//I think the best way to populate this list is to call a service here.
SexList = new List<Sex>
{
new Sex{ID="1",Type = "Female"}
}
}
return View(student);
}
和视图: @Html.BeginForm()
{
<div>
@Html.LabelFor(model => model.FirstName)
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div>
@Html.LabelFor(model => model.LastName)
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
@{
foreach (var sex in Model.SexList)
{
<div>
@Html.RadioButtonFor(model => model.Gender,new { id = "sex" + sex.ID })
@Html.Label("sex" + sex.ID,sex.Type)
</div>
}
}
<input type="submit" value"Submit" />
}
你应该在你的控制器中采取这样的行动,这是提交将发布数据的地方: [HttpPost]
public AcitonResult Index(Student model)
{
if (ModelState.IsValid)
{
//TODO: Save your model and redirect
}
//Call the same service to initialize your model again (cause we didn't post the list of sexs)
return View(model);
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何强制我的ASP.net 2.0应用程序重新编译
- asp.net-mvc – 值不能为空或为空.参数名称:contentPath
- 通过asp.net中的C#将参数传递给CRYSTAL REPORT
- ASP.NET和C#重定向
- asp.net – 如何使用resxresourcewriter写入所有三个值?
- asp.net – 导致GridView无效回发的TemplateField按钮
- asp.net – 你可以从请求变量确定时区吗?
- asp.net-web-api – OAuthBearerAuthenticationMiddleware
- asp.net-mvc – ASP.Net MVC 3中的远程验证:如何在Action方
- asp.net-mvc – 如何在MVC 5中使用用户身份中的角色
推荐文章
站长推荐
- asp.net-core – 更改Asp.net Core中静态文件的标
- 为什么ASP.NET抛出这么多异常?
- ASP.Net AJAX多页面加载功能可能吗?
- ASP.NET Webforms的MVVM模式?
- asp.net – 服务器端声明缓存与Owin身份验证
- asp.net-mvc – 如何在ASP.NET MVC 3应用程序中处
- asp.net-mvc – ASP.NET MVC和Ajax,并发请求?
- asp.net-mvc – 奇怪的错误w / NinjectValidator
- asp-classic – 如何在经典ASP中遍历集合?
- asp.net-mvc-4 – MVC4/DotNetOpenAuth中的自定义
热点阅读
