Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC RadioButtonListFor is always preset

Tags:

asp.net-mvc

I have a radiobuttonlistFor custom adapter working, but if a users form data is reset, and no data has been previously submitted, one of the radio buttons (the first) is always preselected, I want to avoid this, how can I achieve this?

@Html.RadioButtonForSelectList(model => model.ViewModelForThingCreate.ThingTypeID, Model.ViewModelForCarCreate.CarTypeSelectList)

and:

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)
            {
                foreach (SelectListItem Item in ListOfValues)
                {
                    var ID = string.Format("{0}_{1}", MetaData.PropertyName, Item.Value);
                    var Radio = HTMLHelper.RadioButtonFor(Expression, Item.Value, new { id = ID }).ToHtmlString();
                    SB.AppendFormat("<label class=\"radio inline\" for=\"{0}\">{1} {2}</label>", ID, Radio, HttpUtility.HtmlEncode(Item.Text));
                }
            }
            return MvcHtmlString.Create(SB.ToString());
        }

Thanks!

like image 288
williamsandonz Avatar asked Feb 16 '13 03:02

williamsandonz


3 Answers

This is your custom helper with the buttons set to not checked. Try this, I assume it will render all radio buttons unchecked.

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)
            {
                foreach (SelectListItem Item in ListOfValues)
                {
                    var ID = string.Format("{0}_{1}", MetaData.PropertyName, Item.Value);
                    var Radio = HTMLHelper.RadioButtonFor(Expression, Item.Value, new { id = ID }).ToHtmlString();
                    SB.AppendFormat("<label class=\"radio inline\" checked="false" for=\"{0}\">{1} {2}</label>", ID, Radio, HttpUtility.HtmlEncode(Item.Text));
                }
            }
            return MvcHtmlString.Create(SB.ToString());
        }
like image 164
Dave Alperovich Avatar answered Oct 24 '22 19:10

Dave Alperovich


I've just tried your method in mvc3 template and it seems to work fine for me. Basically I've created some Model

 public class IndexModel
 {
     public string ID;
     public IEnumerable<SelectListItem> Elements;
 }

Then created instance and filled values:

var model = new IndexModel()
            {
                ID = "a",
                Elements = 
                       new List<SelectListItem>() { 
                                                    new SelectListItem() { Text = "test1", Value = "1"},
                                                    new SelectListItem() { Text = "test2", Value = "2"}}
            };

In view I've used your extension method

<form>
@(Extensions.RadioButtonForSelectList(Html, x => x.ID, Model.Elements))
<button type="reset">Reset</button>
</form>

All seem perfectly fine after launch. Fields are not selected at load and they're cleared after pressing "Reset" button.

enter image description here

Can you give some more details as I'm not sure if I fully understand what are you trying to achieve :-)

EDIT:

Here's example in plain HTML of radio buttons. They're definitely not filled at the beginning and if you want them to be required add required but by default you can send form without selecting any radio button. Also you can make one checked by adding checked as in second example. Are you using some javascript on client side? Maybe it is causing this side-effect? http://jsbin.com/isadun/1

mz

like image 26
mariozski Avatar answered Oct 24 '22 21:10

mariozski


Unfortunately One radio button must always be checked. That is the unfortunate part about radio buttons; however, You could always add a hidden radio button to your form and set the checked property to true; Have your internal code accept a null or whatever you expect if nothing is selected from it.

Try Setting all of the radio buttons value's to unchecked or false

foreach(button in ButtonGroup){
button.checked = false;
}
like image 45
Robert Avatar answered Oct 24 '22 20:10

Robert