2010-10-04 124 views
0

我真的很希望能夠做到這一點我該如何判斷一個單選按鈕是否被選中? (MVC)

if myRB.Checked = true then 
    return redirecttorout("SomeRoute") 
else 
    someother route 

我習慣的網絡形式,在此我可以在代碼中做到這一點的背後

+1

好吧,如果你想使用mvc,你最好開始做一些教程,書籍等。 – Omu 2010-10-04 18:21:21

回答

0

的mvc像普通的HTML後返回值通過輸入的名稱不是id。在單選按鈕集合中,所有單選按鈕具有相同的名稱。所以,你的東西拿回來與所選擇的單選按鈕的值的單選按鈕的名稱

的代碼最終看起來像

如果(model.radio ==「VA1」) { ... }

0

在表單發佈給您的操作中,需要創建一個字符串var來保存值。

public ActionResult GetRadioButtonValue(string RadioButtonVal) 
{ 

單選按鈕的名稱都需要匹配的觀點,但價值會是什麼被髮送到行動。確保如上所示的字符串與單選按鈕的名稱相匹配。檢查該字符串爲空還是空也是一個好主意。

string.IsNullOrEmpty(RadioButtonVal) 
0

查看

@model MvcApplication2.Models.CountryModel 
@using (Html.BeginForm()) 
{ 
      @Html.RadioButton("Gender", "Male" new { @checked = "checked" }) Male 
      @Html.RadioButton("Gender", "Female")Female 
      <input type="submit" text="submit"/> 
} 

模型

public class CountryModel 
    { 
     public string Gender{ get; set; } 

    } 

控制器

public ActionResult Index() 
{      
    return View(); 
} 
[HttpPost] 
public ActionResult Index(CountryModel model) 
{ 
    //for radio button 
    CountryModel dd = new CountryModel(); 
    string date1 = model.RDB; 
    return View(model); 
} 
相關問題