Click here to Skip to main content
15,892,298 members

MVC3 RadioButton Helper with many independent groups

vitvikt asked:

Open original thread
I want to display under each name from the table radio group relating to this name.
Now, I slightly modified Lanceley’s example (http://jonlanceley.blogspot.ru/2011/06/mvc3-radiobuttonlist-helper.html[^]). But I have to pre-create so many special fields in model as radio units I want to display.
In other case groups are coupled.
How can I solve this problem?
From view:
C#
@model IEnumerable<Sample.Models.IndexViewModel>
…
    int i=0;
    for (i = 0; i < Model.Count();i++ )
    {
     @Html.DisplayTextFor(m => m.ElementAt(i).Name)   
     @Html.RadioButtonForSelectList(m => m.ElementAt(i).TestRadio, Model.ElementAt(i).TestRadioList)
    }

From model:
C#
public class IndexViewModel
    {
        public IEnumerable<SelectListItem> TestRadioList { get; set; }
        public String Name { get; set; }

        [Required(ErrorMessage = "You must select an option for TestRadio")]
        public String TestRadio { get; set; }
        [Required(ErrorMessage = "You must select an option for TestRadio2")]
        public String TestRadio2 { get; set; }
        [Required(ErrorMessage = "You must select an option for TestRadio3")]
        public String TestRadio3 { get; set; }

	…
    }


From helper:
C#
public static class HtmlExtensions
{
    public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression,
        IEnumerable<SelectListItem> listOfValues)
    {
        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, HttpUtility.HtmlEncode(item.Text));
                var radio = htmlHelper.RadioButtonFor(expression, item.Value, new { id = id }).ToHtmlString();

                // Create the html string that will be returned to the client
                // e.g. <input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" /><label for="TestRadio_1">Line1</label>
                sb.AppendFormat("<div class=\"RadioButton\">{0}{1}</div>", radio, label);
                i++;
            }
        }
Tags: C#, MVC3

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900