1

ポスト コントローラー アクションへの 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; }

画像を見ると、テキストボックスのテキストはバインドされていますが、2 つのラジオボタンはバインドされていないことがわかります。 デバッグ結果イメージ

4

1 に答える 1

0

ラジオ入力の name 属性を明示的に変更しています。値は、またはではなく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)
于 2015-12-09T15:34:54.370 に答える