简体   繁体   English

净核心内联剃刀标记空模型

[英]Net Core Inline Razor Markup Null Model

My first go with Razor Pages inline markup. 我第一次使用Razor Pages内联标记。 Running into this weird issue after passing a ViewModel to a PartialView. 在将ViewModel传递给PartialView之后,遇到了这个奇怪的问题。

Of course in my parent page I pass the ViewModel to the PartialView: 当然,在父页面中,我将ViewModel传递给PartialView:

@{Html.RenderPartial("Partial/_RequestView", Model.NewRequest);}

public class IndexModel : PageModel
{
    private readonly IActiveDirectoryClient _activeDirectoryClient;
    private readonly ITravelClient _travelClient;
    public IEnumerable<TravelRequestViewModel> Requests { get; set; }

In the partial view, I have no issue referencing the model in a lambda expression 在局部视图中,在lambda表达式中引用模型没有问题

@Html.HiddenFor(model => model.RequestId)

However when I attempt to reference the Model in razor markup inline the Model is null. 但是,当我尝试在剃刀标记内联中引用该模型时,该模型为null。 Any ideas? 有任何想法吗?

<p>@Model.Name</p>

The NewRequest property is set within the OnGetAsync() method in the parent page 在父页面的OnGetAsync()方法中设置NewRequest属性。

    public async Task<IActionResult> OnGetAsync()
    {
        NewRequest = BuildNewRequest();
        if (NewRequest == null)
            throw new NullReferenceException("Unable to build new travel request");

        return await Task.FromResult(Page());
    }

Answered my own question. 回答了我自己的问题。 MUST remove the @page directive in order for the partial view to work and for the @Model to be recognized. 必须删除@page指令,以使部分视图正常工作并识别@Model。

Kinda bizarre that VS 2017 templates adds this directive for a partial view, I can only assume it's a bug. VS 2017模板在部分视图中添加了该指令,这有点奇怪,我只能假定这是一个错误。

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

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