簡體   English   中英

防止子操作綁定到父ViewData

[英]prevent child action from binding to parent ViewData

我有一個普通視圖(NV)和2個局部視圖(PV1和PV2),PV2嵌套在PV1中,PV1嵌套在NV中。 我遇到的問題是PV1和PV2都有一個可選的五角表,稱為“ isSuccessful”,已添加到視圖包“ ViewBag.IsSuccessful = isSuccessful;”中。 在動作中,由於某種原因,PV1的“ isSuccessful”設置為true PV2的“ isSuccessful”也綁定為true。 這導致PV2過早運行某些JavaScript函數,從而導致其他問題。

代碼(結構和示例)

在控制器中

    public ActionResult NV(int id)
    {
        var model = GetNVModel(id);
        return View(model);
    }

    public ActionResult PV1 (int pv1Id = 0, bool isSuccessful = false)
    {
        var model = GetPV1Model(id);
        ViewBag.IsSuccessful = isSuccessful;
        return PartialView(model);
    }

    public ActionResult PV2 (int pv2Id = 0, bool isSuccessful = false)
    {
        var model = GetPV2Model(id);
        if(model == null){model = new PV2Model();}
        ViewBag.IsSuccessful = isSuccessful;
        return PartialView(model);
    }

觀點

NV.cshtml

     @model NVModel
     <div>
         @if (Model != null && Model.PV1Id != null && Model.PV1Id > 0)
         {<text>
              @Html.Action("PV1", "ControlerName", new { PV1Id = Model.PV1Id, isSuccessful = true})
         </text>}
     </div>

PV1.cshtml

     @model PV1Model
     @{bool isSuccessful = (ViewBag.IsSuccessful != null ? (bool)ViewBag.IsSuccessful : false);}
     <div>
         @if (Model != null && Model.PV2Id != null && Model.PV2Id > 0)
         {<text>
              @Html.Action("PV2", "ControlerName", new { PV2Id = Model.PV2Id })
         </text>}
     </div>
     <script>
         @if (isSuccessful )
         {<text>
              function sayHello()
              {console.log('Hello PV1');}
         </text>}
     </script>

PV2.cshtml

     @model PV2Model
     @{bool isSuccessful = (ViewBag.IsSuccessful != null ? (bool)ViewBag.IsSuccessful : false);}
     <div>
         @if (Model != null)
         {<text>
              @Html.TextBoxFor(model=> model.Message)
         </text>}
     </div>
     <script>
         @if (isSuccessful )
         {<text>
              $(document).ready(function(){
                 console.log($("#Message").val());
              });
         </text>}
     </script>

我不明白的是為什么將PV1的ViewData / ViewBag傳遞給PV2。 我認為不傳遞所有父ViewData / ViewBag是在“ @ Html.Partial”上使用“ @ Html.Action”的部分原因。 我需要知道的是,是否有一個參數需要傳遞給“ @ Html.Action”或需要翻轉一個web.config屬性 ,因為以上內容不僅是一次性的,這是我的一種方式。為了使視圖盡可能靈活,我做了大部分頁面。

我正在使用ASP.NET MVC 4,C#和.NET 4.5

感謝您對這個問題的任何幫助或線索。

動作之間不共享ViewBag,但是路由參數是共享的! 如果在PV2方法內設置了一個斷點並在執行ViewBag.IsSuccessful = isSuccessful;之前檢查了ViewBag,則可以看到此信息ViewBag.IsSuccessful = isSuccessful; 您將看到ViewBag為空,但是該方法收到的參數為true。

問題是您要通過@Html.Action("PV1", "ControlerName", new { PV1Id = Model.PV1Id, isSuccessful = true})將路由參數isSuccessful傳遞給PV1,因此當您調用PV2操作時,路由屬性PV2與當前的合並。 這意味着isSuccessful將傳遞給PV2。

如果檢查Html.Action幫助器的MVC源代碼 ,則會看到路由屬性已與當前屬性合並。 檢查內部ActionHelper方法,其中發生合並:

internal static void ActionHelper(HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, TextWriter textWriter)
{
    ...

    //The route values provided to @Html.Action are merged with the current ones
    RouteValueDictionary additionalRouteValues = routeValues;
    routeValues = MergeDictionaries(routeValues, htmlHelper.ViewContext.RouteData.Values);

    ...
}

如果要防止這種情況,可以在調用PV2時覆蓋route參數。 為此,您可以執行以下操作:

@Html.Action("PV2", "Home", new { isSuccessful = "" })

您還可以使用RouteValueDictionary調用該操作,該操作可讓您將route參數設置為null:

@Html.Action("PV2", "Home", new RouteValueDictionary{{"isSuccessful", null}})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM