mortenstarck
mortenstarck

Reputation: 2803

passing multiple parameter in mvc3

I have an controller that takes 3 paramteres

public ActionResult Directives(string MachineName, int ProjectId, int CompanyId)

Right now the Url looks like this.

/Project/Directives?projectId=41&MachineName=Apple%20Macbook%20Luft

But how do i get it to look like this.

/Project/Directives/41/AppleMacbookAir/

Upvotes: 1

Views: 1276

Answers (2)

Niranjan Singh
Niranjan Singh

Reputation: 18290

You can pass the id as part of the routeValues parameter of the RedirectToAction() method.

return RedirectToAction("Action", new { id = 99 });

This will cause a redirect to Site/Controller/Action/99.

Edit: For customizing url, you have to create custom route as:

routes.MapRoute(  "Project", // Route name
                   "{controller}/{Action}/{ProjectId}/{MachineName}/{CompanyId}", 
                   new { controller = "Project", 
                         action = "Directives", 
                         ProjectId= 0,
                         MachineName= "",
                         CompanyId = 0 
                       }  // Parameter defaults );

Use UrlParameter.Optional if you want any parameter optional. Follow By Stephen Walthe article on ASP.NET MVC Routing

Refer this: RedirectToAction with parameter

Upvotes: 1

Jason Evans
Jason Evans

Reputation: 29186

Try a custom route:

routes.MapRoute(
     "Project",                                              // Route name
     "{Controller}/{Action}/{projectId}/{machineName}/{companyId}",                           
     new { controller = "Project", action = "Directives", projectId = 0, machineName = "", companyId = 0 }  // Parameter defaults
);

http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs

Upvotes: 2

Related Questions