在不使用 Razor Pages 中的处理程序的情况下更改 PageModel 属性
Changing a PageModel property without using handler in Razor Pages
我正在使用 Asp.Net Core Razor Pages.On 页面模型开发 Web 应用程序 我有以下 属性:
public class SchoolModel : PageModel
{
[BindProperty]
public School SchoolEdit { get; set; }
我想在没有表单的情况下单击表单上的按钮时更改 SchoolEdit(学校类型)的徽标 属性,我可以完成此操作吗?
这是剃刀页面代码:
@if (Model.SchoolEdit.Logo != null)
{
<button class="btn btn-danger" asp-page-handler="RemovePhoto">
<span aria-hidden="true">×</span>
</button>
}
后来,我定义了下面的Ajax来改变点击按钮时的属性,但是OnPostRemovePhoto没有被击中!
@section Scripts {
<script>
$(function () {
$("#Click").click(function () {
$.ajax({
type: "POST",
url: "./School?handler=RemovePhoto",
error: function (response) {
alert('hi');
},
success: function (response) {
alert('error');
}
});
});
})
</script>
感谢 Asp.Net 上的同一个帖子,我解决了这个问题。
anti-forgery 标记是 missing.The AJAX 下面的方法在 header 中设置标记。
Here's the answer:
我正在使用 Asp.Net Core Razor Pages.On 页面模型开发 Web 应用程序 我有以下 属性:
public class SchoolModel : PageModel
{
[BindProperty]
public School SchoolEdit { get; set; }
我想在没有表单的情况下单击表单上的按钮时更改 SchoolEdit(学校类型)的徽标 属性,我可以完成此操作吗? 这是剃刀页面代码:
@if (Model.SchoolEdit.Logo != null)
{
<button class="btn btn-danger" asp-page-handler="RemovePhoto">
<span aria-hidden="true">×</span>
</button>
}
后来,我定义了下面的Ajax来改变点击按钮时的属性,但是OnPostRemovePhoto没有被击中!
@section Scripts {
<script>
$(function () {
$("#Click").click(function () {
$.ajax({
type: "POST",
url: "./School?handler=RemovePhoto",
error: function (response) {
alert('hi');
},
success: function (response) {
alert('error');
}
});
});
})
</script>
感谢 Asp.Net 上的同一个帖子,我解决了这个问题。 anti-forgery 标记是 missing.The AJAX 下面的方法在 header 中设置标记。 Here's the answer: