I've read a lot of answers at SO, however it is not fit to me.
I have model:
class SomeOrder
{
public string TypeDrink {get; set;}
}
Controller:
public ViewResult Edit(int id)
{
SomeOrder se = newSomeOrder{ TypeDrink=3 };
return View(se);
}
And view:
@Html.EditorForModel @Html.RadioButtonFor(m=>m.TypeDrink, "1") Tea
@Html.RadioButtonFor(m=>m.TypeDrink, "2") Coffee
@Html.RadioButtonFor(m=>m.TypeDrink, "3") Juice
How to read a chosen value of radiobutton in a [HTTPPOST] method? In my HTTPPOST method the preselected value is stored, not the chosen by user:
[HTTPPOST]
public ViewResult Edit(SomeOrder se)
{
string chosenValue=se.TypeDrink;// always the old selected value
}
Your view includes
@Html.EditorForModel()
before your radio buttons.EditorForModel()
will generate form controls for each property in your model so it will be generating a control for propertyTypeDrink
. Depending on attributes applied to your property, and anyEditorTemplates
you may have, it may generated in a hidden input.Because your form then posts back the name/pair values of the input generated by
EditorForModel
first, the value of the input will be bound to your model and the value of the radio buttons will be ignored by theDefaultModelBinder
.Remove the
EditorForModel
from your view and the model will be bound based on value of the radio buttons.