views:

225

answers:

1

I have a series of URLs that look like

/Catalog/Ajax/Update/{ViewToUpdate}?var1=a&var2=b&var3=c

Currently I've setup several routes - one for each {ViewToUpdate} and what I'd like to do is pass the {ViewToUpdate} to my Action handler so I can condense my code. Instead of:

public ActionResult AjaxUpdateNavigation(string var1, string var2, string var3) {}

I'd like:

public ActionResult AjaxUpdateNavigation(string ViewToUpdate, string var1, string var2, string var3) {}

Here are my current routes:

routes.MapRoute(
"CatalogAjaxNavigation",
"Catalog/Ajax/Update/Navigation",
new { controller = "Catalog", action = "AjaxUpdateNavigation" }
);

How do I set up the route definition correctly to handle both the {ViewToUpdate} string as well as still pass in the querystring?

TIA

+1  A: 

Here's my route:

  routes.MapRoute("TestThing", "test/{ViewToUpdate}", new {controller = "Home", action = "TestQSParams"});

Here's my action:

  public ActionResult TestQSParams(string ViewToUpdate, string var1, string var2)
  {
      TestQSParamsModel m = new TestQSParamsModel {var1 = var1, var2 = var2, ViewToUpdate = ViewToUpdate};
      return View("TestQSParams", m);
  }

Here's my model:

public class TestQSParamsModel
  {
      public string ViewToUpdate { get; set; }
      public string var1 { get; set; }
      public string var2 { get; set; }
  }

Here's my view:

From QS:<br />
  <% foreach(string s in Request.QueryString) 
         Response.Write(string.Format("{0}={1}<br />", s, Request.QueryString[s])); %>
  <br />
  <br />
  From Model:<br />
  <asp:Literal ID="modelvars" runat="server"></asp:Literal>

The view codebehind:

  protected void Page_Load(object sender, EventArgs e)
  {
      modelvars.Text = string.Format("{0}<br />{1}<br />{2}", Model.var1, Model.var2, Model.ViewToUpdate);
  }

My url:

/test/ThisView?var0=douglas&var1=patrick&var2=caldwell

Finally, my result:

  From QS:   
  var0=douglas   
  var1=patrick   
  var2=caldwell

  From Model:
  patrick
  caldwell
  ThisView
D. Patrick