我的RadioButtonFor绑定到我的帖子控制器操作时出现问题。见下文。
主视图 - 调用操作以加载部分视图并使用表单将其包围
@using (Html.BeginForm("FilterPlaceInPriorPosition", "Placements", FormMethod.Post))
{
@Html.Action("AdvancedSearch", "Home", new { Area = "Common", advancedSearchModel = Model.AdvancedSearch })
}
高级搜索部分控制器操作
public ActionResult AdvancedSearch(AdvancedSearch advancedSearchModel)
{
return PartialView("_AdvancedSearch", advancedSearchModel);
}
部分视图 - _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>
发布控制器操作 -FilterPlaceInPriorPosition
[HttpPost]
public ActionResult FilterPlaceInPriorPosition(AdvancedSearch filter)
{
return RedirectToAction("PlaceInPriorPosition", filter);
}
AdvancedSearch.cs类
public class AdvancedSearch
{
public bool MyActiveStudents { get; set; }
public bool AllActiveStudents { get; set; }
如果查看图像,可以看到文本框文本绑定但两个单选按钮没有绑定。 debugging results image
答案 0 :(得分:0)
您明确更改了无线电输入的名称属性。然后,该值将发回WishLister.App
,不 studentTypeRadio
或MyActiveStudents
。由于您的模型上没有任何内容与之匹配,因此该值将被丢弃。
相反,你应该有类似的东西:
AllActiveStudents
然后在你的部分:
public class AdvancedSearch
{
public bool OnlyMyActiveStudents { get; set; } // default will be `false`
}
此外,FWIW在这里使用儿童行动毫无意义。如果您要做的只是将实例传递给局部视图,那么只需@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>
即可完成,而不会产生子操作的所有不必要的开销:
Html.Partial