2012-05-11 192 views
0

我想檢查一個單選按鈕,它基於在@helper方法中的「culture」變量中設置的文化。在調試過程中,我知道變量已設置;它只是不檢查英文或西班牙文單選按鈕。檢查單選按鈕

有人能告訴我如何根據下面的代碼來做到這一點,或者如果有更簡單的代碼方式,那也可以。我正在使用MVC 3 Razor。

@helper selected(string c, string culture) 
    { 
     if (c == culture) 
     { 
      @:checked="checked" 
     } 
    } 

<script type="text/javascript"> 
    $(function() { 
     $("input[type='radio']").click(function() { 
      $(this).parents("form").submit(); 
     }); 

     // highlight selected language 
     $("input[type='radio']:checked").next().css("font-weight", "bold"); 
    }); 
</script> 

@using (Ajax.BeginForm("SetCulture", "Home", new AjaxOptions { UpdateTargetId = "setCulture" })) 
{ 
    <fieldset id="setCulture"> 
     <legend>@Resources.SelectCulture</legend> 
     <input name="culture" id="en-us" value="en-us" type="radio" @selected("en-us", culture) /> 
     <label for="en-us"> 
      English</label> 
     <br /> 
     <input name="culture" id="es-es" value="es-es" type="radio" @selected("es-es", culture) /> 
     <label for="es-es"> 
      Español</label> 
     <br /> 
    </fieldset> 
} 

回答

1

如果你的文化變量等於或者 「EN-US」 或 「ES-ES」 你已經應該工作的內容。我認爲這不等於你的想法。你可以嘗試

<input name="culture" id="testing" value="testing" type="radio" @selected("testing","testing") /> 

來測試你有什麼。

不過我建議你使用RadioButtonFor方法用一個強類型的視圖

public class YourViewModel 
{ 
    public string Culture { get; set; } 
    // Other properties 
} 

@model YourViewModel 

@*Rest of view*@ 

@Html.RadioButtonFor(m => m.Culture, "en-us") 
@Html.RadioButtonFor(m => m.Culture, "es-us")