tags:

views:

137

answers:

3

ok. simple one that is wrapping my brain

I have a method that I have in the controller

public ActionResult Details(string strFirstName, string strLastName)
{
      return View(repository.getListByFirstNameSurname(strFirstName, strLastName)
}

How do i get multiple parameters from the URL to the controller?

I dont want to use the QueryString as it seems to be non-mvc mind set.

Is there a Route? Or Other mechanism to make this work? Or am I missing something altogehter here with MVC

EDIT

the url that I am trying for is

http://site.com/search/details/FirstName and Surname

so if this was classic asp

http://site.com/search/details?FirstName+Surname

But i feel that i have missed understood something which in my haste to get to working code, I have missed the point that there really should be in a put request - and I should collect this from the formcollection.

Though might be worth while seeing if this can be done - for future reference =>

A: 

It is also possible to use FormCollection:

public ActionResult Details(int listId, FormCollection form)
{
  return View(rep.getList(form["firstName"], form["lastName"])
}

Likewise, if the HTTP request contains a form value with the exact same name (case sensitive), it will automatically be passed into the ActionResult method.

Also, just to be clear, there is nothing un-MVC about querystring parameters.

Peter J
+1  A: 

Something like this?:

routes.MapRoute("TheRoute",
    "{controller}/{action}/{strFirstName}/{strLastName}",
    new { controller = "Home", action = "Index", strFirstName = "", strLastName = "" }
);

or:

routes.MapRoute("TheRoute2",
    "people/details/{strFirstName}/{strLastName}",
    new { controller = "people", action = "details", strFirstName = "", strLastName = "" }
);

UPDATED:

This route should be placed before "Default" route:

// for urls like http://site.com/search/details/FirstName/Surname
routes.MapRoute("TheRoute",
    "search/details/{strFirstName}/{strLastName}",
    new { controller = "search", action = "details", strFirstName = "", strLastName = "" }
);

routes.MapRoute("Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" }
);
eu-ge-ne
the second parameter the ActionResult is always null. The url is kind of wrong too /FirstName/Surname which means that there should be a ActionResult for Surname. I am trying to do both?
Could you post an example of url for your action?
eu-ge-ne
updates the question
A: 

Use hidden values in your form

<%= Html.Hidden("strFirstName", Model.FirstName)%>
<%= Html.Hidden("strLastName", Model.LastName)%>

and the model binder will do the binding

public ActionResult Details(string strFirstName, string strLastName)
{
      return View(repository.getListByFirstNameSurname(strFirstName, strLastName)
}
San