views:

361

answers:

6

Hello all, is it possible to go to a different View without changing the URL? For example in my Index View, I have a link to go the the Details View but I would like to keep the URL the same.

Thank you very much, Kenny.

A: 

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 want a link to load up content from a different page without changing the URL, you'll have to use some Ajax to call the server for the content and update the parts of the page you need to change with the new content.

John Sheehan
A: 

I don't know why you would like to do that, but you could have an Ajax.Actionlink which renders the Details View..

There is almost no reason to hide an URL, not sure what you would like to to.. maybe you explain further that someone can give a better approach.

griti
I have a site where clients can visit with their unique id, for example mysite.com/ABC123, which will then render the Index page in this URL. In this Index page content, there's a link to a Details page where they can submit a simple form. I basically would like to keep the URL the same, that's all.
Xuan Vu
+1  A: 

As already mentioned, you could make the Details link an Ajax.ActionLink and use this to change the content of a div.

Failing that, the only other way I can think of doing it is by making your details link a button and POST to your index action. You could apply CSS to the button to make it appear more 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 in a 'fake' param with your post, this is not really a problem though, you can just use a hidden input field for it.

Kirschstein
Thank you, I'll take a look at the Ajax.ActionLink. In doing this, I need to hide the Index content before render the Details, correct?
Xuan Vu
You specify a target div to change the content to, so if all your content specific to the Index action is in a div called 'main' will be instantly replaced by the Details action
Kirschstein
This won't compile, your 2 Index() method signatures are identical, they need to be differentiated somehow.
JonoW
@JonoW, ah yeah - good point.
Kirschstein
A: 

You can use a good old Server.Transfer for this. However, I'd suggest doing it like has been detailed in this SO post. This gives you an easy way to return an ActionMethod from your current action without peppering your code with Server.Transfer() everywhere.

womp
Thank you, I'll take a look at that post.
Xuan Vu
A: 

You can do this by rendering partials- I do this to load different search screens. Sample code is as follows (this is slightly different to my actual code, but you'll get the idea):

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

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

RichardOD
It just a requirement from the client, I personally don't think it's a good idea as well.
Xuan Vu
A: 

I'm using this to successfully load the PartialView to the page, however, my jQuery validation doesn't work with Ajax.ActionLink(), does anyone know how to make it also work with jQuery Validation?

<%= Ajax.ActionLink("More Info", "MoreInfo", "Home", null, new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "Content" }, null)%>

Thank you, Kenny.

Xuan Vu