I am having an issue with my RadioButtonFor's binding to my post controller action. See below.
Main View - Calling an action to load a partial view and surrounding it with a form
@using (Html.BeginForm("FilterPlaceInPriorPosition", "Placements", FormMethod.Post))
{
@Html.Action("AdvancedSearch", "Home", new { Area = "Common", advancedSearchModel = Model.AdvancedSearch })
}
AdvancedSearch Partial Controller Action
public ActionResult AdvancedSearch(AdvancedSearch advancedSearchModel)
{
return PartialView("_AdvancedSearch", advancedSearchModel);
}
Partial View - _AdvancedSearch.cshtml
@model AdvancedSearch
<div class="row">
<div class="col-sm-4">
@Html.TextBoxFor(model => model.Search, new { @class = "form-control no-max-width" })
</div>
<div class="col-sm-8">
@Html.RadioButtonFor(model => model.MyActiveStudents, true, new {Name = "studentTypeRadio"}) <label for="MyActiveStudents">My active students</label>
@Html.RadioButtonFor(model => model.AllActiveStudents, true, new {Name = "studentTypeRadio"}) <label for="AllActiveStudents">All active students</label>
</div>
</div>
Posting controller action -FilterPlaceInPriorPosition
[HttpPost]
public ActionResult FilterPlaceInPriorPosition(AdvancedSearch filter)
{
return RedirectToAction("PlaceInPriorPosition", filter);
}
AdvancedSearch.cs class
public class AdvancedSearch
{
public bool MyActiveStudents { get; set; }
public bool AllActiveStudents { get; set; }
If you look at the image you can see that the textbox text binds over but the two radiobuttons do not. debugging results image
You're explicitly changing the name attribute of the radio inputs. The value, then, will be posted back to studentTypeRadio
, not MyActiveStudents
or AllActiveStudents
. Since there's nothing on your model that matches that, the value is simply discarded.
Instead you should have something like:
public class AdvancedSearch
{
public bool OnlyMyActiveStudents { get; set; } // default will be `false`
}
Then in your partial:
@Html.RadioButtonFor(m => m.OnlyMyActiveStudents, true, new { id = "MyActiveStudents" })
<label for="MyActiveStudents">My active students</label>
@Html.RadioButtonFor(m => m.OnlyMyActiveStudents, false, new { id = "AllActiveStudents" })
<label for="AllActiveStudents">All active students</label>
Also, FWIW, using a child action here is pointless. If all you want to do is pass an instance to the partial view, you can do that with just Html.Partial
without all the unnecessary overhead of a child action:
@Html.Partial("_AdvancedSearch", Model.AdvancedSearch)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.