Derek
Derek

Reputation: 5855

Use Radio Buttons instead of 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();
    }

VIEW

@Html.DropDownList("subscriptionplanx")

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

Upvotes: 1

Views: 4223

Answers (1)

Andre Calil
Andre Calil

Reputation: 7692

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 />
}

Upvotes: 5

Related Questions