简体   繁体   English

在 Visual Studio 2022 中的另一个局部视图(cshtml / cshtml.cs 文件)中访问 Web API 响应 object

[英]Accessing of Web API response object in another partial view (cshtml / cshtml.cs file) in Visual Studio 2022

On click of a button will from a partialview 1 below js function will be called and get the data from the controller and will be redirected to another partialview.单击按钮将从 js 下面的部分视图 1 中调用 function 并从 controller 获取数据,并将被重定向到另一个部分视图。 Since it the controller is in another project and hosted separately, controller is not returning the partialview hence I am redirecting it if the ajax call is success.由于 controller 在另一个项目中并单独托管,因此 controller 没有返回部分视图,因此如果 ajax 调用成功,我将重定向它。

$.ajax({
    type: "POST",
    url: url,
    data: JSON.stringify(paramObj),
    contentType: "application/json; charset=utf-8",
    traditional: true,
    success: function (response) {
        var userObject = response.internalObject;
    window.location.href = url2;
    },
    error: function (response, status, error) {
        if (response.responseText != undefined) {
            const obj = JSON.parse(response.responseText);
            fnShowMessage(obj.DisplayMessage);
        }
    }

}); });

I have the data in "userObject" from the ajax call which needs to be displayed in partialview, but I cannot access it or not sure how to access it.我有来自 ajax 调用的“userObject”中的数据,需要在部分视图中显示,但我无法访问它或不确定如何访问它。

The assigned value in OnGet() method in "partialview2.cshtml.cs" is able to retain in "partialview2.cshtml" file. “partialview2.cshtml.cs”中 OnGet() 方法中的赋值能够保留在“partialview2.cshtml”文件中。 But how to get the values which I got from the ajax call in partialview 1 in code behind of partialview 2.但是如何在部分视图 2 的代码后面的部分视图 1 中获取我从 ajax 调用中获得的值。

public class UserModel : PageModel
{
    [BindProperty]
    public UserObject UserObject { get; set; } = new();
    public void OnGet()
    {
        UserObject.UserName = "man";
    }
}

You can try to return JsonResult in OnGet:您可以尝试在 OnGet 中返回JsonResult

ajax: ajax:

$.ajax({
                type: "POST",
                url: "/Home/Test",
                data: JSON.stringify(paramObj),
                contentType: "application/json; charset=utf-8",
                traditional: true,
                dataType: "json",
                success: function (response) {
                    var userObject = response;
                    window.location.href = url2;
                },
                error: function (response, status, error) {
                    if (response.responseText != undefined) {
                        const obj = JSON.parse(response.responseText);
                        fnShowMessage(obj.DisplayMessage);
                    }
                }
            });

OnGet handler: OnGet 处理程序:

public class UserModel : PageModel
{
    [BindProperty]
    public UserObject UserObject { get; set; } = new();
    public JsonResult OnGet()
    {
        UserObject.UserName = "man";
        return new JsonResult(UserObject);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM