How to call another view from one view in MVC?

Malam Malam 166 Reputation points
2024-06-10T06:12:45.8866667+00:00

In my main MVC project (Proj1) form's menu (Shared_Menu,cshtml), I've added a new menu item to open up a view from another project (proj2) that has been added to the solution.

How do I call the proj2's view when the menu item in proj1 is clicked?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,287 questions
{count} votes

2 answers

Sort by: Most helpful
  1. SurferOnWww 2,171 Reputation points
    2024-06-11T01:54:06.0933333+00:00

    How do I call the proj2's view when the menu item in proj1 is clicked?

    Consider using the Razor Class Library (RCL) as "another project (proj2)":

    enter image description here

    Select [Support pages and views] when creating the RCL project as shown below:

    enter image description here

    You can add controller, model and view to the RCL:

    enter image description here

    Below is a sample code of controller in the RCL project:

    using Microsoft.AspNetCore.Mvc;
    using RazorClassLib.Models;
    
    namespace RazorClassLib.Controllers
    {
        public class RclController : Controller
        {
            public IActionResult Index()
            {
                var model = new RclModel
                {
                    Id = 1,
                    Name = "Apple",
                    Price = 100m
                };
    
                return View(model);
            }
        }
    }
    

    Add project reference from "main MVC project (Proj1)" to the RCL projects:

    enter image description here

    Now you can call the controller in the RCL project as shown below:

    enter image description here


  2. Ping Ni-MSFT 2,795 Reputation points Microsoft Vendor
    2024-06-11T08:24:57.14+00:00

    Hi @Malam Malam,

    If you want to return a view in Project1 which locates in Project 2, you need follow the steps:

    1.Be sure check the Project2 project file should like below(Note Sdk="Microsoft.NET.Sdk.Razor" and be sure add AddRazorSupportForMvc,Microsoft.AspNetCore.App):

    <Project Sdk="Microsoft.NET.Sdk.Razor">
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <RootNamespace>SharedProject</RootNamespace>
    	  <AddRazorSupportForMvc>true</AddRazorSupportForMvc>   
      </PropertyGroup>
    	<ItemGroup>
    		<FrameworkReference Include="Microsoft.AspNetCore.App" />
    	</ItemGroup>
    </Project>
    

    2.Be sure add the Razor view and Controller in Project2 like below:

    User's image

    HomeController in Project2:

    public class HomeController : Controller
    {
        public IActionResult Test()
        {
            return View();
        }
    }
    

    3.Add a reference to Proj2 in Proj1. Right-click on Proj1, choose Add > Project Reference, and select Proj2.

    4.call this api in Project1 like below:

    <a asp-action="Test">Test</a>
    

    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,
    Rena

    0 comments No comments