簡體   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