1
votes

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());
    }
1
What is your model type? - MJK
PageModel, updated original with snippet from parent view code - Tango Down

1 Answers

2
votes

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.