Gino
Gino

Reputation: 39

call razor page from mvc controller

I want to reuse my razor pages instead of having multiple similar views in mvc, I don't want to use a partial view. Thus I want to pass a parameter from a controller to razor page.

In the razor page my input will be something like below

public async Task OnGet(int? x,int?y)

How can i call this function/razor page from my mvc controller?

Upvotes: 1

Views: 6911

Answers (3)

Always_a_learner
Always_a_learner

Reputation: 1304

Controller

public class HomeController : Controller
 {
        // GET: /<controller>/
        public IActionResult Index()
        {           
            return RedirectToPage("/index", new { name = "Arun Kumar"});
        }
 }

Razor Page:

public class IndexModel : PageModel
 {
        public string FullName { get; set; }

        public void OnGet(string name)
        {
            FullName = name;
            ViewData["heading"] = "Welcome  to ASP.NET Core Razor Pages !!";

        }
 }

Razor Page View:

@page
@model RazorMVCMix.Pages.IndexModel



<h1>@ViewData["heading"]</h1>

<h2>@Model.FullName</h2>

Output here:

output

Upvotes: 3

Mike Brind
Mike Brind

Reputation: 30035

In your controller action, use RedirectToPageResult:

return new RedirectToPageResult("/YourRazorPage", new {x = 1, y = 2});

Upvotes: 0

Related Questions