【发布时间】:2016-12-17 03:45:57
【问题描述】:
我正在尝试使用 css 为这些单选按钮设置“选中状态”的样式。除了选中之外,我已经设置了所有其他状态的样式,并且我已经对将它们切换到 input type="radio" 进行了一些挖掘,因为它们看起来更容易设置样式,但还没有找到将它们绑定到模型的方法。见以下代码:
<div class="form-group">
<div class="btn-group" id="roles_select" role="group" data-toggle="buttons">
@Html.LabelFor(model => model.recipientTypeId, "Assign Notification To: ", htmlAttributes: new { @class = "control-label col-md-5" })
<label class="btn btn-primary roles" id="admin">
@Html.RadioButtonFor(model => model.recipientTypeId, "2")Admin
</label>
<label class="btn btn-primary roles" id="candidate">
@Html.RadioButtonFor(model => model.recipientTypeId, "1")Candidate
</label>
<label class="btn btn-primary col-md-push-0 roles" id="manager">
@Html.RadioButtonFor(model => model.recipientTypeId, "3")Manager
</label>
</div>
</div>
来自控制器的代码:
public ActionResult Create()
{
ViewBag.recipientTypeId = new SelectList(db.RecipientType, "Id", "recipientRole");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,notificationType1,recipientTypeId,frequency")] NotificationType notificationType)
{
if (ModelState.IsValid)
{
db.Entry(notificationType).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Create");
}
ViewBag.recipientTypeId = new SelectList(db.RecipientType, "Id", "frequency", "recipientRole", notificationType.recipientTypeId);
return View(notificationType);
}
CSS 被剪断(不工作)
#roles_select input[type="radio"]:checked + label {
background-color:#fff;
}
【问题讨论】:
-
模型绑定和 css 完全不相关。如果它没有约束力,那么它是由于您的代码的其他问题。向您展示模型和控制器代码。
-
input[type="radio"]:checked + label表示<label>元素的样式**紧跟在 **<radio checked />元素之后(您的单选按钮被标签包裹 - 而不是之后)
标签: c# html css asp.net-mvc twitter-bootstrap