asp.net-mvc-3 C 如何在页面提交ASP.Net MVC时捕获哪个单选按钮
发布时间:2023-12-16 16:39:36 所属栏目:asp.Net 来源:DaWei
导读: 这是我的代码,显示我的模型中的两个单选按钮.主要的问题是,当我提交表单时,模型性属性变为null,因为我在表单提交之前选择了一个单选按钮.请帮我找出问题所在.
模特课
public clas
模特课
public clas
这是我的代码,显示我的模型中的两个单选按钮.主要的问题是,当我提交表单时,模型性属性变为null,因为我在表单提交之前选择了一个单选按钮.请帮我找出问题所在. 模特课 public class StudentModel { [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 int SexID { get; set; } public List<Sex> Sex { get; set; } } public class Sex { public string ID { get; set; } public string Type { get; set; } }控制器类 public class HomeController : Controller { [HttpGet] public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; var student = new StudentModel { FirstName = "Rion",LastName = "Gomes",//I think the best way to populate this list is to call a service here. Sex = new List<Sex> { new Sex{ID="1",Type = "Male"},new Sex{ID="2",Type = "Female"} } }; return View(student ); } [HttpPost] public ActionResult Index(StudentModel 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); } }查看代码 @model MvcRadioButton.Models.StudentModel @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.Sex) { <div> @Html.RadioButtonFor(model => model.Sex,new { id = "sex" + sex.ID }) @Html.Label("sex" + sex.ID,sex.Type) </div> } } <input type="submit" value="Submit" /> }如果可能请运行我的代码并告诉我如何实现我的目标,如果有人认为这种情况可以更好地处理只需更改代码然后请告诉我,因为我是MVC的新手. 解决方法 视图模型 public class StudentModel { //properties public bool Sex { get; set; } }视图 @using (Html.BeginForm()) { @Html.RadioButtonFor(model => model.Sex,"false",new { id = "male" }) @Html.Label("male","Male") @Html.RadioButtonFor(model => model.Sex,"true",new { id = "female" }) @Html.Label("female","Female") <button type="submit">OK</button> }调节器 [HttpPost] public ActionResult Index(StudentModel model) { //you can modify "model" however you want here return Content("Sex: " + model.Sex); }(编辑:鲜蔬坊站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – ASP.NET MVC查看引擎解析顺序
- ASP.Net MVC 4窗体与2提交按钮/操作
- asp.net-mvc – 如何在RegularExpression中忽略大小写?
- asp.net-mvc – 在IIS Express中测试SignalR应用程序
- asp.net-mvc – 什么时候使用ViewData而不是ViewModels?
- asp.net-mvc – 如何编辑表格数据(ASP MVC)
- asp.net – 使用WebMethods和session时的最佳实践
- ASP.NET:web.config在哪里/如何缓存?
- asp.net-mvc-4 – MVC 4 DropDownListFor错误 – 没有具有密
- asp.net-mvc – 当Bundling EnableOptimizations为true时,F
推荐文章
站长推荐
- asp.net C App Settings和connectionStrings配置
- asp.net-mvc-4 – 在asp.net mvc 4模型中更改验证
- asp.net-mvc-3 – dataannotations在主键上设置标
- 设置ASP.NET页面不被缓存(客户端/服务器端取消缓
- azure – 当用户存储在外部身份提供程序服务中时
- [转]谈谈技术原则,技术学习方法,代码阅读及其它
- asp.net-mvc-3 – 依赖注入与多个类实现的接口
- linq C ASP.NET Web API GET方法:为单个参数传递
- asp.net-mvc – 未在ELMAH中记录的错误
- asp.net – WebForms:MasterPages中的动态(或绝
热点阅读
- asp.net C MVC4 C ContextDependentView C
- asp.net-mvc C 使用AD的ASP.NET MVC表单Aut
- asp.net-mvc C ASP.NET MVC中的WebApi [Fro
- asp.net-mvc-3 C 如何从ASP.NET MVC#输出中
- asp.net-mvc C 未在ELMAH中记录的错误
- asp.net-mvc C MVC应用程序中的随机数生成
- asp.net-core C 如何使用ASP.NET注册OData
- asp.net-mvc C 已经使用相同的参数类型定义
- ASP.Net C AJAX UpdatePanel中的Javascript
- asp.net C 适用于多个用户的EWS通知中心