[英]Html.RadioButtonFor MVC Binding
我的視圖有一個字符串屬性。 通過操作結果執行get請求並發送到視圖 - 它看起來像這樣:
public ActionResult TeaCoffeView(string teaOrCoffee)
{
MyModel model = new MyModel();
//do some stuff
//teaOrCoffee has two values , if "T" i have
//to make the view as tea radio button selected
//or if it has "C" then coffee radio button selected
return View(model);
}
我的觀點.cshtml
@Html.Label("tea:")
@Html.RadioButtonFor(m => m._teaOrCoffee, Model._teaOrCoffee)
@Html.Label("coffe:")
@Html.RadioButtonFor(m => m._teaOrCoffee, Model._teaOrCoffee)
模型
public string _teaOrCoffee {get; set;}
如何將值與@Html.RadioButtonFor
綁定,以便在加載時它應顯示為選中狀態?
使用@Html.RadioButtonFor()
的第二個參數來設置應該選擇它的值。
@Html.RadioButtonFor(model => model._teaOrCoffee, "T") @Html.Label("tea:")
@Html.RadioButtonFor(model => model._teaOrCoffee, "C") @Html.Label("coffe:")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.