How can I send the model from MVC Controller Action to Razor Page (Asp.Net MVC Core 6.)?

mehmood tekfirst 771 Reputation points
2022-08-12T15:04:01.743+00:00

Hi,

How can I send the model from MVC Controller Action to Razor Page ? I am developing in Asp.Net MVC Core 6.

I added the view

This is the controller action

public async Task<IActionResult> ParamIndex(string path = "/locations/eng/", string apiKey = "")  
        {  
            ViewBag.SrvDtTime = DateTime.Now.ToString("MM/dd/yyyy HH:mm");  
            int franchiseId = 0;  
            int.TryParse(apiKey, out franchiseId);  
            FranchiseWidgetSetting? windgetSetting = null;  
            if (!string.IsNullOrWhiteSpace(path) || !string.IsNullOrWhiteSpace(apiKey))  
            {  
                windgetSetting = await _widgetSettingsRepository.FindFranchiseWidgetSettings(franchiseId, 0, apiKey, string.Empty, path);  
            }             
            return View("~/Views/Param/ParamIndex.cshtml", windgetSetting );  
        }  

this is the page view

@page "{path:string,apiKey:string}"  
@model RentalWidget.Views.Home.ParamWidget.ParamPageModel  
@using   RentalWidget.Models.DBEntities.Franchises  
@model FranchiseWidgetSetting  
@{  
  }  
  
<div id="SearchVehiclePage">  
    @await Html.PartialAsync("ParamSearchCar.cshtml",Model)  
</div>  

The Exception is related to Null Exception and It always return null.

This is the page model but it doesn't receive any debugger. I placed some debugger point in it but the debugger doesn't hit here.

public class ParamPageModel : PageModel  
    {  
        [BindProperty(SupportsGet = true)]  
        public string Path { get; set; } = "";  
  
        [BindProperty(SupportsGet = true)]  
        public string ApiKey { get; set; } = "";  
        public void OnGet()  
        {  
        }  
    }  

Either correct me or bring a good/recommended solution.
Thank you

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2022-08-16T03:12:52.97+00:00

    Hi @mehmood tekfirst ,

    You can try to convert the Razor pages to a MVC views, then in the controller, return the MVC view with the model.

    Code like this:

    Create a ParamModel: 231333-parammodel.txt

    231237-image.png

    Controller: 231361-controller.txt

    231275-231239-image.png

    Right click the Param folder and add a MVC view (ParamIndex.cshtml):

    231286-231236-image.png

    Then the ParamIndex.cshtml like this: 231352-paramindex.txt

    231334-231284-image.png

    If you still want to use Razor Page, you can add the Razor Pages in the Pages folder, then in the controller, use the Redirect method to redirect to the Razor pages.

    Code like this:

    Controller:

    return Redirect("/razorindex?path="+item.Path+"&apikey="+item.ApiKey);  
    

    and in the razor page, the @page like this: 231362-paramrazorindex.txt

    231363-231238-image.png

    Then, in the Razor Page's Get method, receive the parameter, like this:

    231287-231170-image.png

    [Note] in this method, you need to register the Razor Page service and middleware.

    231288-231322-image.png

    The output like this: when access the Home controller Index action, it will redirect to the ParamRazorIndex Razor page.

    231364-231321-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    2 people found this answer helpful.

6 additional answers

Sort by: Most helpful
  1. AgaveJoe 30,491 Reputation points
    2022-08-18T13:15:08.62+00:00

    The problem here which I shared in my above two posts is that passing Data property in ParamPageModel.

    The community has explained several solutions. The problem is you do not understand web design fundamentals and therefore you do not understand the solutions provided by the community. You probably have heard HTTP is stateless? That's the problem you are trying to solve - maintaining state. Maintaining state is problem that exists in every web application that a web developers must deal with. Fortunately, there are many common methods for maintain state in a web application that you get to pick from; cookies, URL, session, database, etc.

    The following line of code is an example of maintaining state in the URL; Path and apiKey.

    return Redirect("parambooking/?path=" + item.Path + "&apikey=" + item.ApiKey);   
    

    The redirect returns a HTTP 301 to the browser. The 301 has a location header that tells the browser where to make the next HTTP GET request which you set to parambooking/?path=somedata&apiKey=somedata. If you want to pass the Data object to parambooking then add parameters to the URL as I explained above. But this might not be the most effective way to maintain state between requests if Data is large. There is a limit to the length of a URL

    A standard approach found in every beginning level tutorial is to pass the Id (primary key) of where the data exists in a database table. The logic on the parambooking fetches the data by Id. In MVC and Razor Page this is called the route parameter.

    Routing to controller actions in ASP.NET Core
    Configure a page route

    Another option is storing Data in Session.

    Session and state management in ASP.NET Core


  2. mehmood tekfirst 771 Reputation points
    2022-08-22T06:10:14.43+00:00

    thank you all. many people participated in this effort.

    I really appreciated. I think everyone's solution should be marked as an Answer.

    but it is difficult for me to decide please mark the answer as you all think which is most close to the answer

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.