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!
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());
}
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.
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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With