在 razor 页面中使用 javascript 更新部分视图
Updating a partial view with javascript in razor pages
我有一个页面 (Index),它列出了 PageModel 列表中的所有对象,如下所示:
page with list of objects
Index.cshtml
@page
@model WAD_Website.Pages.Gods.IndexModel
@{
}
<body>
<label id="lblInformation">Select a God</label>
<div class="searchGod">
<form method="post">
<input type="text" id="txtSearchGod" placeholder="Search for a God..." oninput="SearchGod()" />
</form>
</div>
<div class="godContainer">
@await Html.PartialAsync("_GodsPartial", Model.GodList);
</div>
</body>
<script src="~/js/Gods.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
_GodsPartial.cshtml
@using Models.Gods;
@model List<God>
@foreach (God god in Model)
{
<a class="god" id="@god.Name" asp-page="/Gods/Builds" asp-route-id="@god.Id">
<img class="godImage" src="@god.godIcon_URL" />
<p>@god.Name</p>
</a>
}
加载此索引页面时,神列表由 API 填充,其中 returns 117 个不同的神对象。我希望用户能够搜索此列表。当用户在此文本框中输入文本时:
<input type="text" id="txtSearchGod" placeholder="Search for a God..." oninput="SearchGod()" />
,
它使用 Gods.js 调用 Index.cshtml.cs 中的方法 jQuery
Gods.js
let txtSearchGod = document.getElementById("txtSearchGod");
function SearchGod() {
$.ajax({
type: "POST",
url: '../../Gods?handler=SearchGod',
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: JSON.stringify(txtSearchGod.value),
contentType: "application/json",
dataType: "json"
});
}
Index.cshtml.cs
public async Task OnGet()
{
_godManager = await GodManager.GetInstance();
GodList = new List<God>();
God[] gods = _godManager.GetGodList();
foreach (God god in gods)
{
GodList.Add(god);
}
}
public async Task<IActionResult> OnPostSearchGodAsync()
{
_godManager = await GodManager.GetInstance();
GodList = new List<God>();
God[] gods = _godManager.GetGodList();
string godName = "";
{
MemoryStream stream = new MemoryStream();
await Request.Body.CopyToAsync(stream);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
string requestBody = await reader.ReadToEndAsync();
if (requestBody.Length > 0)
{
godName = JsonConvert.DeserializeObject<string>(requestBody);
if (godName.Length > 0)
{
foreach (God god in gods)
{
if (god.Name.Contains(godName))
{
GodList.Add(god);
}
}
}
else
{
foreach (God god in gods)
{
GodList.Add(god);
}
}
}
}
}
return Partial("_GodsPartial", GodList);
}
一切正常,因为在发送到部分视图页面的列表中只包含过滤后的神。但我就是想不通如何更新这篇 HTML.
<div class="godContainer">
@await Html.PartialAsync("_GodsPartial", Model.GodList);
</div>
我已经查看并尝试了很多可能的解决方案,但都无济于事。有人可以告诉我如何更新 HTML 吗?
您需要向 SearchGod
函数添加 success
或 done
处理程序,您可以在其中访问返回的 HTML 并将其放置在目标元素中:
function SearchGod() {
$.ajax({
type: "POST",
url: '../../Gods?handler=SearchGod',
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: JSON.stringify(txtSearchGod.value),
contentType: "application/json",
dataType: "json"
}).done(function (response){
$('.godContainer').html(response);
});
}
我有一个页面 (Index),它列出了 PageModel 列表中的所有对象,如下所示: page with list of objects
Index.cshtml
@page
@model WAD_Website.Pages.Gods.IndexModel
@{
}
<body>
<label id="lblInformation">Select a God</label>
<div class="searchGod">
<form method="post">
<input type="text" id="txtSearchGod" placeholder="Search for a God..." oninput="SearchGod()" />
</form>
</div>
<div class="godContainer">
@await Html.PartialAsync("_GodsPartial", Model.GodList);
</div>
</body>
<script src="~/js/Gods.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
_GodsPartial.cshtml
@using Models.Gods;
@model List<God>
@foreach (God god in Model)
{
<a class="god" id="@god.Name" asp-page="/Gods/Builds" asp-route-id="@god.Id">
<img class="godImage" src="@god.godIcon_URL" />
<p>@god.Name</p>
</a>
}
加载此索引页面时,神列表由 API 填充,其中 returns 117 个不同的神对象。我希望用户能够搜索此列表。当用户在此文本框中输入文本时:
<input type="text" id="txtSearchGod" placeholder="Search for a God..." oninput="SearchGod()" />
,
它使用 Gods.js 调用 Index.cshtml.cs 中的方法 jQuery
Gods.js
let txtSearchGod = document.getElementById("txtSearchGod");
function SearchGod() {
$.ajax({
type: "POST",
url: '../../Gods?handler=SearchGod',
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: JSON.stringify(txtSearchGod.value),
contentType: "application/json",
dataType: "json"
});
}
Index.cshtml.cs
public async Task OnGet()
{
_godManager = await GodManager.GetInstance();
GodList = new List<God>();
God[] gods = _godManager.GetGodList();
foreach (God god in gods)
{
GodList.Add(god);
}
}
public async Task<IActionResult> OnPostSearchGodAsync()
{
_godManager = await GodManager.GetInstance();
GodList = new List<God>();
God[] gods = _godManager.GetGodList();
string godName = "";
{
MemoryStream stream = new MemoryStream();
await Request.Body.CopyToAsync(stream);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
string requestBody = await reader.ReadToEndAsync();
if (requestBody.Length > 0)
{
godName = JsonConvert.DeserializeObject<string>(requestBody);
if (godName.Length > 0)
{
foreach (God god in gods)
{
if (god.Name.Contains(godName))
{
GodList.Add(god);
}
}
}
else
{
foreach (God god in gods)
{
GodList.Add(god);
}
}
}
}
}
return Partial("_GodsPartial", GodList);
}
一切正常,因为在发送到部分视图页面的列表中只包含过滤后的神。但我就是想不通如何更新这篇 HTML.
<div class="godContainer">
@await Html.PartialAsync("_GodsPartial", Model.GodList);
</div>
我已经查看并尝试了很多可能的解决方案,但都无济于事。有人可以告诉我如何更新 HTML 吗?
您需要向 SearchGod
函数添加 success
或 done
处理程序,您可以在其中访问返回的 HTML 并将其放置在目标元素中:
function SearchGod() {
$.ajax({
type: "POST",
url: '../../Gods?handler=SearchGod',
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: JSON.stringify(txtSearchGod.value),
contentType: "application/json",
dataType: "json"
}).done(function (response){
$('.godContainer').html(response);
});
}