简体   繁体   中英

Net Core Inline Razor Markup Null Model

My first go with Razor Pages inline markup. Running into this weird issue after passing a ViewModel to a PartialView.

Of course in my parent page I pass the ViewModel to the 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

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

However when I attempt to reference the Model in razor markup inline the Model is null. Any ideas?

<p>@Model.Name</p>

The NewRequest property is set within the OnGetAsync() method in the parent page

    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.

Kinda bizarre that VS 2017 templates adds this directive for a partial view, I can only assume it's a bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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