Button Group of Radio Buttons没有设置任何带Bootstrap 3的活动按钮
我试图用MVC和Bootstrap 3创建一组很好的单选按钮:
选择一个选项后:
我将值存储在数据库中但是当我再次显示View时,没有选择任何内容:
该模型是:
[Table("QuotePiecePrinting")] public partial class QuotePiecePrinting { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Display(Name = "Tipo de Impressão")] public int PrintType { get; set; } }
观点是:
@Html.LabelFor(model => model.PrintType, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-4 col-md-3" })
当我再次显示视图时,为什么不反映所选选项的存储值?
该值正确存储在数据库中。
只要您将正确的值设置为要传递给视图的视图模型的PrintType属性,这应该可以正常工作(它将选择单选按钮)。
public ActionResult Index() { return View( new QuotePiecePrinting { PrintType=2}); }
但是,如果您希望css类处于活动状态,则可以将该css类显式应用于文档就绪事件上所选单选按钮的父标签。 所以将它添加到页面的脚本中。
$(function(){ $("input[name='PrintType']:checked").closest("label.btn").addClass("active"); });
尝试下面的代码来使用默认选择function呈现引导单选按钮组
视图:
@{ foreach (var item in Model.GenderList) { } } @Html.ValidationMessageFor(m => m.Gender)
性别枚举:
public enum Gender { Male = 0, Female = 1, RatherNotSay = 2 }
GenderList
它是Model类中的IEnumerable属性,可以使用Gender Enum在Get控制器操作中填充它。
public IEnumerable> GenderList { get; set; }
视图将呈现如下: