繁体   English   中英

单选按钮不与局部视图绑定

[英]RadioButtonFor not binding with partial view

我的RadioButtonFor绑定到我的后控制器操作时遇到问题。 见下文。

主视图 -调用操作以加载部分视图并用表单将其包围

@using (Html.BeginForm("FilterPlaceInPriorPosition", "Placements", FormMethod.Post))
{
    @Html.Action("AdvancedSearch", "Home", new { Area = "Common", advancedSearchModel = Model.AdvancedSearch })
}

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; }

如果查看图像,可以看到文本框文本已绑定,但两个单选按钮没有绑定。 调试结果图

您正在显式更改单选输入的名称属性。 然后,该值将回发到studentTypeRadio而不是 MyActiveStudentsAllActiveStudents 由于模型上没有与之匹配的值,因此该值将被简单丢弃。

相反,您应该具有以下内容:

public class AdvancedSearch
{
    public bool OnlyMyActiveStudents { get; set; } // default will be `false`
}

然后在您的部分:

@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>

同样,FWIW在这里使用子动作是没有意义的。 如果您要做的只是将实例传递给部分视图,则只需使用Html.Partial即可,而无需子操作的所有不必要开销:

@Html.Partial("_AdvancedSearch", Model.AdvancedSearch)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM