Link to home
Start Free TrialLog in
Avatar of WorknHardr
WorknHardr

asked on

MVC Action Params Null?

I've read other posts and still doing something wrong, params always null. Help!

- view -
@Html.ActionLink("Make a new file...", "MakeFile", "FileMaker", new { id = "pdf", type = "sales" }) 

- action - 
 public ActionResult MakeFile(string id, string type) // both strings null
 {
 }

- global.asax - 
 routes.MapRoute(
            "12",
            "FileMaker/MakeFile/{id, type}", new { controller = "FileMaker", action = "MakeFile", id = "", type = "" }
            )

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Try:

routes.MapRoute(
            "12",
            "FileMaker/MakeFile?id={id}&type={type}", new { controller = "FileMaker", action = "MakeFile", id = "", type = "" }
            )

Open in new window

Avatar of WorknHardr
WorknHardr

ASKER

Error:
The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
Parameter name: routeUrl
It looks to me like the default MVC route mapping would work fine.  Why are you defining a route?  Routing does not use the query string this way.  But the default binding will handle it without problems.

Try removing that routing definition from global.asax.   Put some code in your MakeFile method and click your action link.
Solution:
1. Removed global.asax maproute for this actionlink
2. Added a 5th actionlink param (null)
3. Now it works.
4. I think MVC routing is screwy

@Html.ActionLink("Make a new file...",
           "MakeFile", "FileMaker", new { id = "pdf", type = "sales" }, null)
The last parameter, which should be an object containing HTML attributes that get added to the link, should not really be needed either, especially as you are just passing null.  What URL is generated in the link with and without it?
I don't get it either, MVC 3 routing is screwy

Without  the 5th param (null) the url ends with ?Length=10
Interesting, because it looks like it is automatically adding what should be an HTML attribute.

If you right click the ActionLink statement and select "Go to definition" does it show a definition in System.Web or has someone overridden it with a custom definition in the project.
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thx for helping my figure this out...

Solution: (5th param =  ,NULL

  @Html.ActionLink("Make a new file...",
           "MakeFile", "FileMaker", new { id = "pdf", type = "sales" }, null)
No problem.  Some of those overloads are tricky with string, string, string, object etc, and because of the object parameters, they can accept anything.  I should have checked that first.