2012-09-15 35 views
5

Tôi không thể đặt nút radio mặc định thành "Đã kiểm tra"! Tôi đang sử dụng @ Html.RadioButtonFor:Đặt @ Html.RadioButtonFor as Checked theo mặc định

<div id="private-user"> 
    @Html.RadioButtonFor(m => m.UserType, "type1", new { @class="type-radio" , **@Checked="checked"** }) 1 
</div> 
<div id="proff-user"> 
    @Html.RadioButtonFor(m => m.UserType, "type2", new { @class="type-radio" }) 2 
</div> 

Có thể thiết lập một nút radio như cheched sử dụng @ Html.RadioButtonFor?

Thx

Trả lời

5

Trong hành động điều khiển của bạn thiết lập giá trị của UserType tài sản trên mô hình quan điểm của bạn với giá trị tương ứng:

public ActionResult SomeAction() 
{ 
    MyViewModel model = ... 

    // preselect the second radio button 
    model.UserType = "type2"; 
    return View(model); 
} 

và theo quan điểm của bạn:

<div id="private-user"> 
    @Html.RadioButtonFor(m => m.UserType, "type1", new { @class="type-radio" }) 1 
</div> 
<div id="proff-user"> 
    @Html.RadioButtonFor(m => m.UserType, "type2", new { @class="type-radio" }) 2 
</div> 
0

Trong hàm tạo của Mô hình Xem của bạn, chỉ cần khởi tạo thuộc tính cho giá trị bạn muốn.

public YourViewModel(){ 
    UserType = "type1"; 
} 

Cách đăng lại của bạn sẽ sử dụng bất kỳ giá trị nào bạn muốn và bạn không phải đặt giá trị trong bộ điều khiển.

Các vấn đề liên quan