Razor Pages - 在 ajax 在服务器上获取请求后访问 razor 视图绑定变量值
Razor Pages - Access razor view binded variable value after ajax get request on server
我正在尝试在视图中绑定此变量,因此我不需要通过 ajax 调用发送它,不确定这是否可能,但遵循它看起来像的逻辑。
<script>
$(function ()
{
GetStocksByUAP();
});
function GetStocksByUAP() {
@{
Model.Sector = "UAP1";
}
$.get('/dashboard/Index?handler=StocksBySector', function (response) {
swal('OK', 'Dados carregados', 'success');
console.log(response);
});
}
</script>
cs 文件
[BindProperty]
public string Sector { get; set; }
public IndexModel(IConfiguration config)
{
_config = config;
}
public async Task<IActionResult> OnGetStocksBySectorAsync()
{
IDbConnection con = new SqlConnection(_config.GetConnectionString("DefaultConnection"));
var result = await con.QueryAsync<DashboardViewModel>("GetStocksByUAP", new { UAP = Sector });
return new JsonResult(result);
}
总而言之,我尝试使用页面上声明的字符串 Sector 并在 ajax 在服务器上获取请求后访问它在视图上绑定的值。虽然不确定这是否可能
如果您想在处理程序中处理一个由 AJAX 请求使用 GET
方法调用的值,您需要将该值传递给处理程序。您可以将其作为查询字符串值或路由数据值来执行,但无论哪种方式,它都必须在 URL 中传递。
如果要将值绑定到 PageModel 属性,必须在 BindProperty
属性中指定 SupportsGet = true
:
[BindProperty(SupportsGet=true)]
public string Sector { get; set; }
在此处详细了解模型绑定在 Razor Pages 中的工作原理:https://www.learnrazorpages.com/razor-pages/model-binding#binding-data-from-get-requests
我正在尝试在视图中绑定此变量,因此我不需要通过 ajax 调用发送它,不确定这是否可能,但遵循它看起来像的逻辑。
<script>
$(function ()
{
GetStocksByUAP();
});
function GetStocksByUAP() {
@{
Model.Sector = "UAP1";
}
$.get('/dashboard/Index?handler=StocksBySector', function (response) {
swal('OK', 'Dados carregados', 'success');
console.log(response);
});
}
</script>
cs 文件
[BindProperty]
public string Sector { get; set; }
public IndexModel(IConfiguration config)
{
_config = config;
}
public async Task<IActionResult> OnGetStocksBySectorAsync()
{
IDbConnection con = new SqlConnection(_config.GetConnectionString("DefaultConnection"));
var result = await con.QueryAsync<DashboardViewModel>("GetStocksByUAP", new { UAP = Sector });
return new JsonResult(result);
}
总而言之,我尝试使用页面上声明的字符串 Sector 并在 ajax 在服务器上获取请求后访问它在视图上绑定的值。虽然不确定这是否可能
如果您想在处理程序中处理一个由 AJAX 请求使用 GET
方法调用的值,您需要将该值传递给处理程序。您可以将其作为查询字符串值或路由数据值来执行,但无论哪种方式,它都必须在 URL 中传递。
如果要将值绑定到 PageModel 属性,必须在 BindProperty
属性中指定 SupportsGet = true
:
[BindProperty(SupportsGet=true)]
public string Sector { get; set; }
在此处详细了解模型绑定在 Razor Pages 中的工作原理:https://www.learnrazorpages.com/razor-pages/model-binding#binding-data-from-get-requests