簡體   English   中英

如何在MVC5中由ajax調用的Partial View中引用外部javascript?

[英]How to reference outside javascript in a Partial View called by ajax in MVC5?

我已經研究了有關MVC 3MVC 4的此主題的答案,並且希望能找到針對MVC 5的解決方案。我有一個視圖,可通過ajax加載部分視圖。 在視圖中,我在其中定義了所有JavaScript邏輯( @section Scripts ),因為我的局部視圖中沒有任何邏輯。 問題是,在我的局部視圖中,我希望在成功后調用視圖中定義的javascript函數。

以下是我的看法和局部看法

MyView.cshtml

@section Scripts
{
    @Scripts.Render("~/plugins/myplugins")
    <script>
        function callFunctionOutsideThisPartialView() {
        //... This doesn't get called
        }
    </script>
}

MyPartialView.cshtml

@using (Ajax.BeginForm("MyAction", "MyController",
new AjaxOptions
{
    HttpMethod = "POST", 
    InsertionMode = InsertionMode.Replace, 
    UpdateTargetId = "divForPartialView", 
    OnSuccess = "callFunctionOutsideThisPartialView()"
}, new { @class = "form-inline" }))
{
    // Partial View here
}

在MVC 5中訪問部分視圖之外的javascript函數的正確方法是什么?

如果我沒記錯的話,只需要在普通視圖中渲染局部視圖並通過ajax調用加載該普通視圖即可,還可以將jQuery添加到普通視圖中。

暫無
暫無

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

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