0
votes

Hello and thank you in advance for any help you can give me.

I'm working with .NET Core 3.1

I have a Razor Class Library with Razor Pages.

They are partial pages that are included inside views of a Web MVC project also .NET Core 3.1

The razor pages of the RCL are inside the "\Pages" folder in a "\Shares" subfolder.

I load the partial pages like this: <partial name="_SpatialTest" />

It works properly.

But I need to send several parameters to some of the Razor Pages of the RCL from the views that includes, and there comes my problem, since as it is not a hyperlink I cannot add the parameters in the routing, or in the Body, or in the querystring... as suggested here: https://www.learnrazorpages.com/razor-pages/routing

I have tried it as view-data, as model and data and it gives me several errors but it never works, like suggested here: https://www.learnrazorpages.com/razor-pages/model-binding

I've got it set up like this:

Razor Page in RCL ->

cshtml.cs:

namespace SpatialUnitsRclTest.Pages.Shared
{
    public class _SpatialTestModel : PageModel
    {
        [BindProperty(SupportsGet = true)]
        public string NameTest { get; set; }

        public void OnGet(send parameters)
        {
            // do something with the parameters and create the Razor Page ViewModel ...
        }
    }
}

cshtml:

@page
@model SpatialUnitsRclTest.Pages.Shared._SpatialTestModel
<h5>Test RCL partial @Model.NameTest</h5>
<div id="mapid" style="min-height: 180px;"></div>

View (cshtml) in Web MVC project ->

<div style="height: 200px; width: 200px;">
    @{ var partialModel = new SpatialUnitsRclTest.Pages.Shared._SpatialTestModel { NameTest = "pruebaaaaaaaaaaa" }; }
    <partial name="_SpatialTest" model="@partialModel"  />
</div>

The error is:

NullReferenceException: Object reference not set to an instance of an object. AspNetCore.Pages_Shared__SpatialTest.get_Model() AspNetCore.Pages_Shared__SpatialTest.ExecuteAsync() in _SpatialTest.cshtml + Test RCL partial @Model.NameTest Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context) Microsoft.AspNetCore.Mvc.TagHelpers.PartialTagHelper.RenderPartialViewAsync(TextWriter writer, object model, IView view) Microsoft.AspNetCore.Mvc.TagHelpers.PartialTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output) Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.g__Awaited|0_0(Task task, TagHelperExecutionContext executionContext, int i, int count) AspNetCore.Views_Home_Index.ExecuteAsync() in Index.cshtml + Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable statusCode) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable statusCode) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable statusCode) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result) Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|29_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Can you give me a hand?

Thanks a lot. Greetings.

1
No one has any idea why it fails? Thanks a lot!!!sanmolhec

1 Answers

0
votes

If you'd like to render partial view (contained in your RCL) with specified Model data within your MVC project view page, please refer to the following example.

Razor View _SpatialTest1 (in RCL)

@model RclTest.Models.SpatialTestModel
@{
    //code logic here
}

<h1>_SpatialTest1</h1>
<h5>Test RCL partial @Model.NameTest</h5> 

Note: create a Razor View _SpatialTest1 in your RCL, not Razor Page.

SpatialTestModel class (in RCL)

public class SpatialTestModel
{
    public string NameTest { get; set; }
}

View page (in MVC project)

@{
    var partialModel = new RclTest.Models.SpatialTestModel { NameTest = "pruebaaaaaaaaaaa" };
}

<partial name="_SpatialTest1" model="@partialModel" />

Test Result

enter image description here