Reputation: 39
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
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:
Upvotes: 3
Reputation: 30035
In your controller action, use RedirectToPageResult:
return new RedirectToPageResult("/YourRazorPage", new {x = 1, y = 2});
Upvotes: 0
Reputation: 4621
Controllers are just classes you can call action like methods from another controller.
How to call another controller Action From a controller in Mvc
https://forums.asp.net/t/1645726.aspx?how+to+call+a+controller+action+from+another+controller
Upvotes: 0