Search code examples
asp.net-coreasp.net-core-mvcasp-net-core-spa-services

Is there any solution about asp.net core mvc switch route without refresh page like angular route reuse strategy


i just want to achieve the following results,switch route without refresh page enter image description here

supplement: i want to keep the page state enter image description here


Solution

  • You can achieve this function partial view.

    Here is a simple demo for your reference:

    HomeController:

      public class HomeController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }
    
            public IActionResult FirstTab(string param)
            {
              return PartialView("_FirstTab",param);
            }
    
        }
    

    Index.cshtml:

    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
        $(function () {
            $("#tabs").tabs();
        });
    </script>
    <div id="tabs">
        <ul>
            <li><a href="#tabs-1">Page1</a></li>
            <li><a href="#tabs-2">Page2</a></li> 
        </ul>
        <div id="tabs-1"> 
            @Html.Partial("_FirstTab", "Page1")
        </div>
        <div id="tabs-2">
            @Html.Partial("_FirstTab", "Page2")
        </div>
    </div>
    

    _FirstTab.cshtml partial view:

    @model string
    
    <h1>This is the @Model Tab</h1>
    <input id="Text1" type="text" />
    

    Test result:

    enter image description here