Reputation: 29583
I have an IList in my model. Which i am displaying as radio buttons. But when i submit the form the value is not correct and the model state is not valid and where the value for the selected radio button should be there is 'Count =0'
This the option in model:
[Display(Name = "My enquiry is regarding: *")]
public IList<Industry> A1_EnquiryRegarding { get; set; }
controller: populate list:
Industry blank = new Industry();
blank.Id = 0;
blank.Name = "Other";
IList<Industry> industryList = manager.GetIndustries();
industryList.Insert(industryList.Count, blank);
EnquiryModel.A1_EnquiryRegarding = industryList;
html:
<td>
<div class="editor-label">
<b> @Html.LabelFor(m => m.A1_EnquiryRegarding)</b>
</div>
<div class="editor-field">
@foreach (var radiobutton in Model.A1_EnquiryRegarding) {
@Html.RadioButtonFor(m => m.A1_EnquiryRegarding, radiobutton.Name)
<label>@radiobutton.Name</label>
<br></br>
}
@Html.ValidationMessageFor(m => m.A1_EnquiryRegarding)
</div>
</td>
where am i goign wrong? why am i not getting the correct selected value back?
Edit:
[HttpPost]
public ActionResult EnquiryForm(Enquiry Enquiry)
{
Upvotes: 0
Views: 3238
Reputation: 12366
When you post back, your collection of complex object is not recreated. Instead, there is only one string value passed with the selected value of the radio. Your model for the update action should only include one name.
Implement your radiolist as follows:
@foreach (var radiobutton in Model.A1_EnquiryRegarding) {
@Html.RadioButton("selectedIndustry", radioButton.Name);
}
All your radio buttons should have the same name, but different values. That way, when you call your Post action, you just search for parameter "selectedIndustry".
[HttpPost]
public ActionResult MyPostAction(string selectedIndustry) {
}
Upvotes: 1