簡體   English   中英

剃刀頁面-ajax在服務器上獲取請求后,訪問剃刀視圖綁定的變量值

[英]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在服務器上獲取請求后訪問在視圖上綁定的值。 不確定是否可以

如果要在使用GET方法被AJAX請求調用的處理程序中處理值,則需要將該值傳遞給處理程序。 您可以將其用作查詢字符串值或路由數據值,但無論哪種方式,都必須在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM