简体   繁体   English

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

[英]RadioButtonFor not binding with partial view

I am having an issue with my RadioButtonFor's binding to my post controller action. 我的RadioButtonFor绑定到我的后控制器操作时遇到问题。 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 AdvancedSearch部分控制器操作

public ActionResult AdvancedSearch(AdvancedSearch advancedSearchModel)
    {

       return PartialView("_AdvancedSearch", advancedSearchModel);
    }

Partial View - _AdvancedSearch.cshtml 局部视图 -_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 发布控制器操作 -FilterPlaceInPriorPosition

[HttpPost]
        public ActionResult FilterPlaceInPriorPosition(AdvancedSearch filter)
        {
            return RedirectToAction("PlaceInPriorPosition", filter);
        }

AdvancedSearch.cs class AdvancedSearch.cs类

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 . 然后,该值将回发到studentTypeRadio而不是 MyActiveStudentsAllActiveStudents 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. 同样,FWIW在这里使用子动作是没有意义的。 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即可,而无需子操作的所有不必要开销:

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

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

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