简体   繁体   中英

Making Request to Controllers - ASP.NET Core

I have a form to fetch data from my database based on the asp-action. The form looks like below

HTML

<form  id="user__collection" asp-action="Admins" asp-controller="User" method="get">
    <input name="collection"
    type="text"
    id="user_search"
   />
    <button  type="submit">
    </button>
</form>

Now i have two menu tabs named Admins and Users . When i am in the admin tab and i search for a surname, it fetches from the database and display both Admins and Users under the respective tabs, that is the logic and works as expected

Now when i am in users tab and i search for a surname, after the page reloads, it goes back to the Admin tab, and i have to select the Users tab to see the results.

My issue here is, is there a way i can make sure after the page reloads, it stays in the users tab without having to go to the Admin tab first since that is where i made the request from.

My idea was to find a way to use JS to set asp-action based on the URL.

Admin Menu url - localhost:8000/admins Users Menu url - localhost:8000/users

UserController

 public async Task<IActionResult> Users([BindRequired] string collection)
 {
      //fetch users from database and display
 }

 public async Task<IActionResult> Admins([BindRequired] string collection)
 {
      //fetch admins from database and display
 }

U can set a Referrer link to take U back to the previous page . Proceed as below :

ViewBag.Url = System.Web.HttpContext.Current.Request.UrlReferrer;

And then in the controller use this :

return Redirect(ViewBag.Url);

And if U are using html tabs ,the solution may not be working because html tabs don't have urls.

To solve this U have to Create partial views and now U have their urls.

Good Luck

也许你可以在控制器上使用: return RedirectToAction("Users");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM