如何通过HTML类来创建一个MVC视图一个单选按钮列表(剃刀语法) [英] How can I create a RadioButtonList in a MVC View via HTML class ( Razor syntax )

查看:369
本文介绍了如何通过HTML类来创建一个MVC视图一个单选按钮列表(剃刀语法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示在单选按钮列表,一些这样的事我的名单:
@ Html.RadioButtonList(FeatureList,新的SelectList(ViewBag.Features))
但是当你知道有在HTML Helper类没有RadioButtonList的类,当我使用:
@ Html.RadioButton(FeatureList,新的SelectList(ViewBag.Features))
它显示我一个空白的名单!
//控制器codeS:

 公众的ActionResult规则()
        {            ViewBag.Features =(从Db.Features m其中m.ParentID == 3选择m.Name);
            返回查看();        }


解决方案

Html.RadioButton 不采取(字符串的SelectList)参数,所以我想空白列表预期;)

您可以1)

使用的foreach 在模型中的单选按钮值,并使用 Html.RadioButton(字符串对象)超载来迭代你的价值观

  //选项可能就是一个List<串GT;或其他适当的
//为您Feature.Name数据类型
@foreach(在Model.Options VAR myvalue的){
    @ Html.RadioButton(nameOfListmyvalue的)
}

或2)

写的名单了自己的助手方法 - 可能会是这个样子(我从来没有写一个像这样,所以你的里程可能会有所不同)

 公共静态MvcHtmlString单选按钮列表(此的HtmlHelper帮手,
    串NameOfList,列表与LT;字符串> RadioOptions){    StringBuilder的SB =新的StringBuilder();    //把一个类似这里的foreach
    的foreach(在RadioOptions VAR myOption){
        sb.Append(helper.RadioButton(NameOfList,myOption));
    }    返回新MvcHtmlString(sb.ToString());
}

然后调用你的新助手在你看来像(假设Model.Options仍然是列表或其他适当的数据类型)

  @ Html.RadioButtonList(nameOfList,Model.Options)

I need to show my list in a RadioButtonList , some thing like this: @Html.RadioButtonList("FeatureList", new SelectList(ViewBag.Features)) But as you know there is no RadioButtonList class in HTML Helper class and when I use : @Html.RadioButton("FeatureList", new SelectList(ViewBag.Features)) it shows me a blank list! // Controller codes :

 public ActionResult Rules()
        {

            ViewBag.Features = (from m in Db.Features where m.ParentID == 3 select m.Name);
            return View();

        }

解决方案

Html.RadioButton does not take (string, SelectList) arguments, so I suppose the blank list is expected ;)

You could 1)

Use a foreach over your radio button values in your model and use the Html.RadioButton(string, Object) overload to iterate your values

// Options could be a List<string> or other appropriate 
// data type for your Feature.Name
@foreach(var myValue in Model.Options) {
    @Html.RadioButton("nameOfList", myValue)
}

or 2)

Write your own helper method for the list--might look something like this (I've never written one like this, so your mileage may vary)

public static MvcHtmlString RadioButtonList(this HtmlHelper helper, 
    string NameOfList, List<string> RadioOptions) {

    StringBuilder sb = new StringBuilder();

    // put a similar foreach here
    foreach(var myOption in RadioOptions) {
        sb.Append(helper.RadioButton(NameOfList, myOption));
    }

    return new MvcHtmlString(sb.ToString());
}

And then call your new helper in your view like (assuming Model.Options is still List or other appropriate data type)

@Html.RadioButtonList("nameOfList", Model.Options)

这篇关于如何通过HTML类来创建一个MVC视图一个单选按钮列表(剃刀语法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆