2012-05-11 193 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")