0

I'm using bootstrap with tabs and controlling navigation with a fragment identifier in the URL. This is my current usage from a Controller that is calling a view:

return RedirectResult(Url.Action("Dashboard", 
                                  new { id = account.Id }) + "#tab_NotesTab");

This works great, but now I need to pass the model from my action. Normally I pass it with the standard syntax:

return View(model);

How do I do both? I want to pass both a model and the fragment identifier to the View.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Josh
  • 8,219
  • 13
  • 76
  • 123

1 Answers1

1

Sounds like a solution for TempData!

public ActionResult Index()
{
  // var model = ...
  TempData["model"] = model;
  return new RedirectResult(Url.Action("Dashboard", 
    new { id = account.Id }) + "#tab_NotesTab");      
}

public ActionResult Dashboard()
{
  var model = (MyModelType)TempData["model"];

  return View(model);
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • I'm not following your example. The Dashboard view doesn't pass the fragment identifier, just the model. – Josh Jan 23 '15 at 15:58
  • BTW, I've tried your example to see if the fragment remained, but I ran into a problem that page didn't reload. I can step through the actions called and step through the cshtml view page, but when it is done processing the view, it the browser doesn't actually update. – Josh Jan 23 '15 at 17:33
  • Sounds like a caching issue maybe. – Erik Philips Jan 23 '15 at 17:46
  • I put a no cache attribute "[OutputCache(Location=System.Web.UI.OutputCacheLocation.None)]" on both actions with no luck. – Josh Jan 23 '15 at 17:48
  • In your example, if I didn't have this no refresh issue, would the fragment "#tab_NotesTab" stay appended to the URL string when the Dashboard called the View? Basically it would be passed through? – Josh Jan 23 '15 at 17:50
  • The StackOverflow answer to [Including hash values in ASP.NET MVC URL routes](http://stackoverflow.com/a/5568381/209259) seems to indicate that the hash should reach the client. But realize that the hash is never sent to the server from browsers (the opposite direction). – Erik Philips Jan 23 '15 at 22:08
  • Finally got it working, The hash does survive when the model is loaded. – Josh Jan 23 '15 at 22:49
  • Shouldn't it be `return *new* RedirectResult(...`? As in `return new RedirectResult(...`, not `return RedirectResult(...`. – kim3er Apr 08 '16 at 11:42