![](/img/trans.png)
[英]Model binding a form created from configurations in ASP.NET Core Razor Pages/MVC
[英]ASP.NET Core MVC with Razor Pages (can't return object from a different model)
我正在使用作為Razor頁面模型(cs)文件的一部分創建的方法來調用Azure表。 結果是TenantEntityModel
類型的。 我只想在我的視圖中顯示這些結果。
是否可以這樣做,如果可以,我的Razor頁面模型需要做什么?
我的Razor模型中的方法具有以下調用:
TenantEntityModel Result = await azureTableConnection.TenantLookupAsync(azureTableConnection, domainName, id);
我以為我可以在視圖中使用@Model.Result.DomainName
,但是由於Result
在我的Razor模型中不存在,因此沒有對該對象的引用。 所以我嘗試將其添加到我的Razor模型中,如下所示:
public TenantEntityModel Result { get; }
這使我可以在“剃刀”視圖中訪問“ Result
的屬性。 如果我在return Page()
語句之前放置一個斷點,則可以看到Result
對象具有我想要的屬性。 但是,當渲染視圖時, Model
對象的Result
屬性為null,我得到:
NullReferenceException:對象引用未設置為對象的實例。
我也嘗試添加BindProperty
,如下所示:
[BindProperty]
public TenantEntityModel Result { get; }
...但是那也不起作用。 因此,我對如何獲取查詢結果,如何使用來自不同類的模型填充對象,然后使用該對象的結果呈現頁面感到困惑。
查看代碼:
@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>
}
控制器代碼:
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();
}
為了在Razor Page
View
和PageModel
之間傳遞模型,您需要為已定義的模型設置值public TenantEntityModel Result { get; set; }
public TenantEntityModel Result { get; set; }
public TenantEntityModel Result { get; set; }
代替defineing一個新的對象的TenantEntityModel Result
。
嘗試將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();
}
將您的PageModel代碼更改為此。 您正在創建局部變量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.