0

在这里,我需要isChecked在 Dictionary 项目中添加值。

我尝试直接添加,但出现以下错误:

 " CS1501: No overload for method 'Add' takes 1 arguments"*. Is there any alternate way to add ischecked in Idictionary.   

我的代码是:

 @{
     bool val = false;
     var isChecked = (val == true) ? new { @Checked = "checked" }  : null;
  }
@Html.RadioButtonFor(x => x.Status, "Male",new Dictionary<string,object>{{"id","GenderRdoButton"},{isChecked}})
4

3 回答 3

1

这段代码对我有用,

@{
                    bool vals = false;
                    var dictionaryObj = new Dictionary<string, object>();
                    dictionaryObj.Add("id", "goodId");
                    dictionaryObj.Add("class", "sadd");
                    if(vals){
                        dictionaryObj.Add("checked", "checked");   
                    }
                }
                 @Html.RadioButtonFor(x => x.Status, "Good",dictionaryObj) Good
于 2013-07-19T05:51:29.453 回答
0

您可以使用带有 IDictionary 的重载:

    @{
        bool val = true;
        var attributes = new Dictionary<string, string>() { {"id", "GenderRdoButton"}};
        if (val)
            attributes.Add("checked","checked");
    }
   @Html.RadioButtonFor(x => x.valid, "male", attributes) Male
于 2013-07-18T00:32:48.817 回答
0

尝试将val值传递给这样的方法:

@Html.RadioButtonFor(x => x.valid, val, new { @id = "GenderRdoButton" })
于 2013-07-18T00:19:24.230 回答