使用单选按钮代替DropDownList [英] Use Radio Buttons instead of DropDownList

查看:84
本文介绍了使用单选按钮代替DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我能够从数据集中动态填充@Html.DropDownList().下面的代码可以很好地解决这个问题.

Currently I am able to dynamically populate @Html.DropDownList() from a dataset. The code below works fine for this.

CONTROLLER

public static IEnumerable<SelectListItem> myList
    {
        get
        {
            ReportAPI.ReportsAPI ws = new ReportAPI.ReportsAPI();
            DataSet ds = ws.GetReportDataSet(userguid, reportguid);
            DataTable dt = ds.Tables[0];

            List<SelectListItem> list = new List<SelectListItem>();
            foreach (DataRow dr in dt.Rows)
            {
                list.Add(new SelectListItem
                {
                    Text = dr["text"].ToString(),
                    Value = dr["value"].ToString(),
                });
            }
            return list;
        }
    }

public ActionResult NewSubscriber()
    {

        ViewData["subscriptionplanx"] = myList;

        return View();
    }

查看

@Html.DropDownList("subscriptionplanx")

现在,我不想使用DropDownList,而是想使用单选按钮.我该怎么办?

Now, instead of using a DropDownList, I want to use radio buttons instead. How would I do this?

推荐答案

使用HtmlHelper可能是最优雅的解决方案. 无论如何,如果您正在寻找一些简单的方法,请尝试以下操作:

Using a HtmlHelper is probably the most elegant solution. Anyway, if you're looking for something straighforward, try this:

@foreach(SelectListItem item in (IEnumerable<SelectListItem>)ViewData["subscriptionplanx"])
{
    <input type="radio" name="subscriptionplanx" value="@item.Value" />@item.Text <br />
}

这篇关于使用单选按钮代替DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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