如何更新/刷新 ASP.NET Core Razor 视图中的 ViewData?

问题描述 投票:0回答:1

我已按照评论中给出的建议创建了一个

PartialView
。运行调试器时,我可以看到
@Model
在某个时间点被
reply
变量的内容填充。但是,它从未显示在页面上!我在这里做错了什么?

// Pages\Shared\_ReplyPartial.cshtml

<h1>Response</h1>

<p>@Model</p>

这就是我的 PageModel 的样子

// Dependencies.cshtml.cs

public class DependenciesModel : PageModel
{
    ... 
    
    public PartialViewResult OnGetCreateBlob()
    {
        ... 

        var response = _client.PutAsync(url, new StringContent(DateTime.UtcNow.ToString())).Result;     
        var reply = $"StatusCode: {(int)response.StatusCode}";

        return Partial("_ReplyPartial", reply);
    }
}

最后是页面本身

// Dependencies.cshtml

@page
@model AppInsightsDemo.Pages.DependenciesModel
@{
    ViewData["Title"] = "Dependencies";
}
<h1>@ViewData["Title"]</h1>

@section scripts
{
    <script>
        $('#createBlob').on('click', () => {
            $.ajax({
                url: '?handler=CreateBlob'
            });
        });
    </script>
}


<button class="btn btn-primary mb-1" id="createBlob">Create Blob</button>

<partial name="_ReplyPartial">


原帖

请考虑以下代码,我在其中进行 AJAX 调用以从后面的代码中调用

OnGetCreateBlob()

//// Site.cshtml

@page
@model Demo.Pages.SiteModel
@{
    ViewData["Title"] = "Dependencies";
}
<h1>@ViewData["Title"]</h1>

@section scripts
{
    <script>
        $(document).click(e => {
            if (e.target.id == "createBlob") {
                $.ajax({
                    url: '?handler=CreateBlob'
                });    
            }
        });
    </script>
}

<button class="btn btn-primary" id="createBlob">Create Blob</button>

<h1>Response</h1>

<div id="response">
    @ViewData["Reply"]
</div>
//// Site.cshtml.html 

public class SiteModel : PageModel
{
    ... 

    public void OnGetCreateBlob()
    {
        ...

        var response = _client.PutAsync(url, new StringContent(DateTime.UtcNow.ToString())).Result;

        var statusCode = (int)response.StatusCode;
        var reply = $"StatusCode: {statusCode}";

        ViewData["Reply"] = reply;
    }
}

问题是,

ViewData
没有在视图上更新。

我尝试在执行 AJAX 调用后使用

window.location.reload()
刷新窗口,但是似乎
ViewData
的内容丢失了。

如果我执行

$("#response").load(location.href + " #response");
并重新加载
div
,则不会出现任何内容。

那么如何在后面的代码填充

ViewData
之后显示它的内容呢?

javascript c# jquery asp.net-core razor-pages
1个回答
1
投票

你可以尝试在ajax之后替换

<partial name="_ReplyPartial">
。这是一个演示:

依赖项.cshtml:

@page
@model AppInsightsDemo.Pages.DependenciesModel
@{
    ViewData["Title"] = "Dependencies";
}
<h1>@ViewData["Title"]</h1>

@section scripts
{
    <script>
        $('#createBlob').on('click', () => {
            $.ajax({
                url: '?handler=CreateBlob',
                success: function (data) {
                    $("#partial").html(data);
                }
            });
        });
    </script>
}


<button class="btn btn-primary mb-1" id="createBlob">Create Blob</button>
<div id="partial">
    <partial name="_ReplyPartial">
</div>

你也可以在ajax后替换Site.cshtml中的

@ViewData["Reply"]
。这里有一个演示:

站点.cshtml:

@page
@model Demo.Pages.SiteModel
@{
    ViewData["Title"] = "Dependencies";
}
<h1>@ViewData["Title"]</h1>

@section scripts
{
    <script>
        $(document).click(e => {
            if (e.target.id == "createBlob") {
                $.ajax({
                    url: '?handler=CreateBlob',
                     success: function (data) {
                        $("#response").html(data);
                    }
                });    
            }
        });
    </script>
}

<button class="btn btn-primary" id="createBlob">Create Blob</button>

<h1>Response</h1>

<div id="response">
    @ViewData["Reply"]
</div>

站点.cshtml.cs:

public class SiteModel : PageModel
{
    ... 

    public IActionResult OnGetCreateBlob()
    {
        ...

        var response = _client.PutAsync(url, new StringContent(DateTime.UtcNow.ToString())).Result;

        var statusCode = (int)response.StatusCode;
        var reply = $"StatusCode: {statusCode}";

        ViewData["Reply"] = reply;
        return Content(reply);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.