如何在ASP.NET Core 2.0中使用Razor Pages处理Ajax请求
问题描述:
下面我正在尝试在CustomerModel Razor Pages中创建一个Post Request to Action方法/处理程序。如何在ASP.NET Core 2.0中使用Razor Pages处理Ajax请求
RazorPage名称是 “Customer.cshtml”
从Ajax请求绑定DROPDOWNLIST
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
<select class="form-control" id="CustomerID"
name="CustomerID"
asp-for="Customer.CustomerID"
asp-items="@(new SelectList(string.Empty,"CustomerID", "Name"))">
</select>
Ajax请求
我也试过只调用HANDL呃,但它显示错误
<script type="text/javascript">
$(document).ready(function()
{
$.ajax({
type: 'GET',
// we are calling json method
// /Pagename/ActionMethod
// /Pagename/Handler(OnGetListofCustomer)
url: '/Customer/OnGetListofCustomer',
contentType: "application/json",
success: function (data) {
$("#CustomerID").append('<option value="' + "0" + '">' + "Select State" + '</option>');
debugger;
$.each(data, function (i, state) {
$("#CustomerID").append('<option value="' + state.CustomerID + '">' + state.Name + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve states.' + ex);
}
});
});
PageModel
错误,同时使Ajax请求
答
我认为你不能访问这样的剃须刀页面操作方法。你可以这样试试吗?
$.getJSON("/Customer?handler=ListofCustomer",function(data){
//Do something with the data.
});
因为访问除默认使用onGET或我们需要处理onPOST等方法之外的任何方法,其中运行时间在内部映射到方法。
+0
感谢您的回答@Anuraj – Saineshwar
还记得设置防伪令牌看到:https://stackoverflow.com/questions/46410716/example-ajax-call-back- to-an-asp-net-core-razor- –