【发布时间】:2015-10-17 17:12:15
【问题描述】:
在编辑页面中,我有两个性别单选按钮,一旦用户选择性别男性或女性,该值应保存在数据库中。
我尝试了所有提出的解决方案,但没有一个可行。
这是查看页面:
<div class="form-group">
@Html.LabelFor(model => model.IsMale, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="radioLeft col-sm-3">
@Html.RadioButtonFor(model => model.IsMale, "Male", new { @checked = true })Male
@Html.RadioButtonFor(model => model.IsMale, "Female", new { @checked = false })Female
@Html.ValidationMessageFor(model => model.IsMale, "", new { @class = "text-danger" })
</div>
</div>
这里是控制器编辑方法:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit( WebUser webUser)
{
if (ModelState.IsValid)
{
db.Entry(webUser).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(webUser);
}
【问题讨论】:
-
'...but none of works' - 什么你尝试过什么?
-
你说的“但是没有用”是什么意思?您能否向我们提供更多信息,了解当您选中单选按钮时会发生什么?
-
感谢您的快速回复。当我点击保存按钮时,它不起作用,它显示“值'女性'对性别无效。”,为什么?
-
听起来你模型中的任何数据字段都不支持该字符串
-
我的模型是:public Nullable
IsMale { get;放; }
标签: c# asp.net-mvc radio-button edit