Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# MVC3
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:
@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:
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:
    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++;
                }
            }
Posted 19-Nov-12 19:36pm
vitvikt436

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 359
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 185
4 PIEBALDconsult 149
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100