简体   繁体   English

带有Razor页面的ASP.NET Core MVC(无法从其他模型返回对象)

[英]ASP.NET Core MVC with Razor Pages (can't return object from a different model)

I'm making a call to Azure tables in a method I've created as part of the Razor page model (cs) file. 我正在使用作为Razor页面模型(cs)文件的一部分创建的方法来调用Azure表。 The results are of type TenantEntityModel . 结果是TenantEntityModel类型的。 I'd like to simply display those results in my view. 我只想在我的视图中显示这些结果。

Is it possible to do this and if so, what needs to be in my Razor page model? 是否可以这样做,如果可以,我的Razor页面模型需要做什么?

The method in my Razor model has the following call: 我的Razor模型中的方法具有以下调用:

TenantEntityModel Result = await azureTableConnection.TenantLookupAsync(azureTableConnection, domainName, id);

I thought I could just use @Model.Result.DomainName in the view however there is no reference to this object as Result didn't exist in my Razor model. 我以为我可以在视图中使用@Model.Result.DomainName ,但是由于Result在我的Razor模型中不存在,因此没有对该对象的引用。 So I tried adding it to my Razor model as: 所以我尝试将其添加到我的Razor模型中,如下所示:

public TenantEntityModel Result { get; }

This allows me to access the properties of the Result in my Razor view. 这使我可以在“剃刀”视图中访问“ Result的属性。 If I put a break point before the return Page() statement I can see the Result object has the properties I'm after. 如果我在return Page()语句之前放置一个断点,则可以看到Result对象具有我想要的属性。 However when the view is rendered, the Model object's Result property is null and I get: 但是,当渲染视图时, Model对象的Result属性为null,我得到:

NullReferenceException: Object reference not set to an instance of an object. NullReferenceException:对象引用未设置为对象的实例。

I've also tried adding BindProperty as follows: 我也尝试添加BindProperty ,如下所示:

[BindProperty]
public TenantEntityModel Result { get; }

...but that didn't work either. ...但是那也不起作用。 So I'm at a loss on how to take the results of a query, populate an object with a model from a different class, then render a page with the results of that object. 因此,我对如何获取查询结果,如何使用来自不同类的模型填充对象,然后使用该对象的结果呈现页面感到困惑。

View code: 查看代码:

@page
@model ApiKeyModel
@if (@Model.Result.ApiKey == null)
            {
                //show the button
                <button name="CreateApiKey" type="submit" class="btn btn-primary">Create new API Key</button>
            }

Controller code: 控制器代码:

public class ApiKeyModel : PageModel
{
    private readonly UserManager<UserRegistrationExtension> _userManager;
    private readonly ILogger<PersonalDataModel> _logger;

    [BindProperty]
    public TenantEntityModel Result { get; set; }

    public ApiKeyModel(
        UserManager<UserRegistrationExtension> userManager,
        ILogger<PersonalDataModel> logger)
    {
        _userManager = userManager;
        _logger = logger;
    }

    public async Task<IActionResult> OnGet()
    {
        // need to validate if account exists first
        var user = await _userManager.GetUserAsync(User);
        if (user == null)
        {
            return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
        }

        var domainName = user.Office365DomainName;
        var id = user.Id;
        AzureTableConnection azureTableConnection = new AzureTableConnection();
        TenantEntityModel Result = await azureTableConnection.TenantLookupAsync(azureTableConnection, domainName, id);

        return Page();
    }

For passing model between View and PageModel in Razor Page , you need to set the value for the defined model public TenantEntityModel Result { get; set; } 为了在Razor Page ViewPageModel之间传递模型,您需要为已定义的模型设置值public TenantEntityModel Result { get; set; } public TenantEntityModel Result { get; set; } public TenantEntityModel Result { get; set; } instead of defineing a new object TenantEntityModel Result . public TenantEntityModel Result { get; set; }代替defineing一个新的对象的TenantEntityModel Result

Try to change OnGet() to: 尝试将OnGet()更改为:

public async Task<IActionResult> OnGet()
{
    // need to validate if account exists first
    var user = await _userManager.GetUserAsync(User);
    if (user == null)
    {
        return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
    }

    var domainName = user.Office365DomainName;
    var id = user.Id;
    AzureTableConnection azureTableConnection = new AzureTableConnection();
    Result = await azureTableConnection.TenantLookupAsync(azureTableConnection, domainName, id);

    return Page();
}

Change your PageModel code to this. 将您的PageModel代码更改为此。 You were creating a local variable TenantEntityModel Result instead of assigning value to your property. 您正在创建局部变量TenantEntityModel Result而不是为属性分配值。

public class ApiKeyModel : PageModel
{
    private readonly UserManager<UserRegistrationExtension> _userManager;
    private readonly ILogger<PersonalDataModel> _logger;

    [BindProperty]
    public TenantEntityModel Result { get; set; }

    public ApiKeyModel(
        UserManager<UserRegistrationExtension> userManager,
        ILogger<PersonalDataModel> logger)
    {
        _userManager = userManager;
        _logger = logger;
    }

    public async Task<IActionResult> OnGet()
    {
        // need to validate if account exists first
        var user = await _userManager.GetUserAsync(User);
        if (user == null)
        {
            return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
        }

        var domainName = user.Office365DomainName;
        var id = user.Id;
        AzureTableConnection azureTableConnection = new AzureTableConnection();
        Result = await azureTableConnection.TenantLookupAsync(azureTableConnection, domainName, id);

        return Page();
    }
}

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

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