简体   繁体   English

无法通过ASP.NET Core中的JQuery ActionResult调用加载部分视图

[英]Unable to load Partial View via JQuery ActionResult call in ASP.NET Core

I am stuck in this problem for hours if not days. 我在这个问题上停留了好几个小时,甚至连几天都没有。

I can start the ActionResult that returns PartialView("MODEL") but I am unable to render actual View into the div inside an Index cshtml page. 我可以启动返回PartialView(“ MODEL”)的ActionResult,但是无法将实际View呈现到Index cshtml页面内的div中。

Here is what I am trying: 这是我正在尝试的:

Controller: 控制器:

...
public ActionResult Calendar()
    {
        PopulateCalendarAsync();
        return PartialView(_calendarViewModel);
    }
...

Index.cshtml : Index.cshtml:

<div id="calendarDiv" ></div>

script inside Index: 索引内的脚本:

<script type="text/javascript">
$(document).ready(function (e) {
    $.get('@Url.Action("Calendar","Home")', {}, function (result) {
        $("#calendarDiv").load("~/Home/Calendar");

    });
});

When Index page loads, script calls the ActionResult Calendar() with no problems. 加载“索引”页面后,脚本将毫无问题地调用ActionResult Calendar()。

The problem is that div is not being updated. 问题是div没有被更新。

my Calendar.cshtml page is aside Index page in Home folder. 我的Calendar.cshtml页面位于主文件夹中的索引页面旁边。

What am I missing here? 我在这里想念什么?

The result parameter of the $.get request will contain the html output of the calendar partial view. $.get请求的result参数将包含日历部分视图的html输出。
Assign it to the <div> via $("#calendarDiv").html(result); 通过$("#calendarDiv").html(result);将其分配给<div> $("#calendarDiv").html(result);

Full code: 完整代码:

$(document).ready(function (e) {
    $.get('@Url.Action("Calendar","Home")')
        .then(function (result) {
             $("#calendarDiv").html(result);
         })
        .fail(function (xhqr, status, error) {
            alert(status);
            alert(JSON.stringify(xhqr));
        })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM