[英]How can I update/refresh ViewData in my ASP.NET Core Razor View?
更新
好吧,仍然没有运气。 任何帮助是极大的赞赏!!
我已经按照评论中给出的建议创建了一个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
的内容呢?
您可以尝试在 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>
也可以在Site.cshtml中ajax后替换@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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.