假設你有一個像這樣
public Class CheckOutViewModel
{
public string SelectedPaymentType { set; get; }
public IEnumerable<SelectItems> PaymentTypes { set; get; }
}
一個視圖模型,你在你的GET
操作方法來設置該PaymentTypes收集併發送至被強類型到CheckOutViewModel
public ActionResult Checkout()
{
var vm=new CheckOutViewModel
vm.PaymentTypes=GetPaymentTypes(); //gets a list of SelectItems
return View(vm);
}
和視圖在您查看
@model CheckOutViewModel
@using(Html.BeginForm())
{
foreach (var paymentItem in Model.PaymentTypes)
{
@Html.RadioButtonFor(mbox => mbox.SelectedPaymentType,
paymentItem.ID.ToString())
@paymentItem.Name
}
<input type="submit" value="Save" />
}
As suming GetPaymentTypes()
方法將返回一個SelectItems
的列表,用於您在數據庫中的記錄。
這將爲您提供具有相同名稱值(SelectedPaymentType)的單選按鈕。所以只能選擇一個。
在你POST
動作,你可以通過檢查SelectedPaymentType屬性值
[HttpPost]
public ActionResult Checkout(CheckOutViewModel model)
{
//check the value of model.SelectedPaymentType
}
只需使用jQuery允許一個複選框可以同時選擇讀取選定的值。 – 2012-08-07 15:14:46