簡體   English   中英

MVC使用Ajax渲染部分視圖

[英]MVC render partial view using ajax

我需要你的幫助。

我正在嘗試使用ajax渲染部分視圖。 但是失敗了。 這是代碼:

 //I have action to get data
 public ActionResult ReturnPartial()
    {
                return PartialView("_LogIn");
    }

 //here is ajax
     <p onclick="Func1()">Log In</p>

<div id="here"></div>

<script>
    function Func1() {
        $.ajax({
            type: "GET",
            url: '@(Url.Action("ReturnPartial", "Registration"))',
            success: function (data) {
                $("#here").html(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("Failed to retrieve view");
            }
        });
    }
</script>

當我單擊“登錄”時,它將更改消息:“無法檢索視圖”;

感謝您的關注,請幫助我

當您在RegistrationController具有此操作方法時

public ActionResult ReturnPartial()
{
    return PartialView("_LogIn");
}

確保_LogIn.cshtml位於“ Views/Shared文件夾或“ Views/Registration文件夾中。

如果要將_LogIn.cshtml放在Views/Partial文件夾中,則可以通過指定如下所示的完整路徑來訪問部分視圖

public ActionResult ReturnPartial()
{
    return PartialView("~/Views/Partial/_LogIn.cshtml");
}

您不應警告一般消息“無法檢索視圖”。 您應該至少已經向xhr.status和xhr.responseText發出警報,這樣您才能知道您是否無法訪問服務器(客戶端/網址錯誤)或服務器由於服務器錯誤而無法返回響應。

另外,請確保您的常規ErrorHandling(HandleErrorAttribute或Application_Error)記錄了所有錯誤,以便您可以找到所有服務器錯誤及其詳細信息。

暫無
暫無

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

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