ASP.NET MVC - change to another view without changing url

Can I navigate to another view without changing the url? For example, in my index view, I have a link to go to the detail view, but I would like to keep the url the same.

Thanks a lot Kenny.

+2


source to share


5 answers


As mentioned, you can link the Details link with Ajax.ActionLink

and use it to change the content of the div.

Otherwise, the only way I can do this is for your data to associate a button POST

with your index action. You can apply CSS to the button to make it look like a normal html link.

public class HomeController : Controller {

   public ActionResult Index() {
      return View("Index");
   }

   [AcceptVerbs(HttpVerbs.Post)]
   public ActionResult Index(int hiddenInputFieldId) {
      return View("Details");
   } 
}

      



EDIT:

Based on JonoW's comment, you'll have to pass a "fake" parameter with your post, but that's not a problem, you can just use a hidden input field for it.

+1


source


You can return the same view from multiple controller actions, but each controller action requires a unique url:

public class HomeController : Controller {
    public ActionResult Index() {
        return View("home");
    }

    public ActionResult About() {
        return View("home");
    }
}

      



If you need a link to download content from another page without changing the url, you will have to use some kind of Ajax to call the server for the content and update the parts of the page that you need to change with the new content.

0


source


I don't know why you would like to do this, but you might have an Ajax.Actionlink that displays the details.

You have almost no reason to hide the URL, but not sure what you would like. You might explain that someone can give a better approach.

0


source


For this, you can use the good old Server.Transfer

. However, I would suggest doing it, as detailed in this SO post . This gives you an easy way to return an ActionMethod from your current activity without dropping your code with the help Server.Transfer()

everywhere.

0


source


You can do this by editing partials - I do this to load various search screens. Sample code looks like this (this is slightly different from my actual code, but you get the idea):

<% Html.RenderPartial(Model.NameOfPartialViewHere, Model.SomeVM); %>

      

Personally, though, I don't understand why you don't just change the URL?

0


source







All Articles