Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Html帮助程序-RadioButtonForSelectList添加名称_C#_Asp.net Mvc - Fatal编程技术网

C# Html帮助程序-RadioButtonForSelectList添加名称

C# Html帮助程序-RadioButtonForSelectList添加名称,c#,asp.net-mvc,C#,Asp.net Mvc,我正在使用下面的Html帮助程序,但是可选的name参数没有按预期工作 因此,在某些情况下,我希望使用htmlAttribute发送一个自定义名称作为参数,就像我们使用id一样 不知何故,名称htmlAttribute没有被输出,所以我仍然得到默认的自动生成名称 下面的完整代码块 public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>( this HtmlHelper<

我正在使用下面的Html帮助程序,但是可选的name参数没有按预期工作

因此,在某些情况下,我希望使用
htmlAttribute
发送一个自定义名称作为参数,就像我们使用id一样

不知何故,名称
htmlAttribute
没有被输出,所以我仍然得到默认的自动生成名称

下面的完整代码块

public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, 
            TProperty>> expression,
            IEnumerable<SelectListItem> listOfValues, 
            string name = "")
{
    var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    var sb = new StringBuilder();

    if (listOfValues != null)
    {
       // Create a radio button for each item in the list 
       foreach (SelectListItem item in listOfValues)
       {
          // Generate an id to be given to the radio button field 
          var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);

          // Create and populate a radio button using the existing html helpers 
          var label = htmlHelper.Label(id, item.Text);
          string radio;

          if (string.IsNullOrEmpty(name))
          {
              radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id}).ToHtmlString();
          }
          else
          {
              radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id, name = name }).ToHtmlString();
          }



// Create the html string that will be returned to the client e.g. 

           sb.AppendFormat("<div class=\"RadioButton\">{0}{1}</div>", radio, label);
       }
    }

    return MvcHtmlString.Create(sb.ToString());
}
public static MvcHtmlString radiobuttonnforselectlist(
这个HtmlHelper HtmlHelper,
表情表情,
IEnumerable值列表,
字符串名称=”)
{
var metaData=modelmetada.FromLambdaExpression(表达式,htmlHelper.ViewData);
var sb=新的StringBuilder();
if(listOfValues!=null)
{
//为列表中的每个项目创建一个单选按钮
foreach(在listOfValues中选择ListItem项)
{
//生成要给单选按钮字段的id
var id=string.Format(“{0}{1}”,metaData.PropertyName,item.Value);
//使用现有html帮助程序创建并填充单选按钮
var label=htmlHelper.label(id,item.Text);
弦乐电台;
if(string.IsNullOrEmpty(name))
{
radio=htmlHelper.RadioButtonOn(表达式,item.Value,new{id=id}).ToHtmlString();
}
其他的
{
radio=htmlhelp.RadioButtonFor(表达式,item.Value,new{id=id,name=name}).ToHtmlString();
}
//创建将返回给客户端的html字符串,例如。
sb.附件格式(“{0}{1}”,收音机,标签);
}
}
返回MvcHtmlString.Create(sb.ToString());
}

谢谢

htmlHelper.radiobutton(expression,item.Value)
应该会自动为你生成它。你能澄清@Murali吗?我使用了类似于
htmlHelper.radiobutton的(expression,myValue,new{id,disabled=“disabled”})。ToHtmlString()
而且它会自动为我生成名称。@Murali在回答问题时写道:“在某些情况下,我想发送一个自定义名称作为参数”。我不知道如果不写自己的助手,你怎么能做到这一点,但你为什么要这样做?它将破坏MVC可能使用名称字段进行的任何绑定。