views:

1460

answers:

2

I have ran into an odd problem with the ActionLink method in ASP.NET MVC Beta. When using the Lambda overload from the MVC futures I cannot seem to specify a parameter pulled from ViewData.

When I try this:

<%= Html.ActionLink<PhotoController>(p => p.Upload(((string)ViewData["groupName"])), "upload new photo") %>

The HTML contains a link with an empty URL.

    <a href="">upload new photo</a>

However if I hard code the parameter, like this:

<%= Html.ActionLink<PhotoController>(p => p.Upload("groupA"), "upload new photo") %>

The output contains an actual URL.

    <a href="/group/groupA/Photo/Upload">upload new photo</a>

I assume this probably has something to do with the visibility and availability of the ViewData, and it not being there when the Lambda gets evaluated by the internals of the framework. But that is just a guess.

Am I doing something incorrect in the first sample to cause this, or is this some short of bug?

Update: I am using the latest version of the MVC futures. It has been pointed out that this works for some people. Since it doesn't work for me this makes me think that it is something specific to what I am doing. Does anybody have any suggestion for what to look at next, because this one really has me stumped.

+2  A: 

Have you updated your version of the Microsoft.Web.Mvc.dll where the Strongly typed actionlink resides.

Apparently this dll has been updated for the Beta release. The function may have been slightly modified.

I just tried this

<%= Html.ActionLink<HomeController>(x=>x.Search((string)ViewData["search"]), "search?") %>

and it worked fine.

Schotime
My version of Microsoft.Web.Mvc is 1.0.31003.0. I downloaded it from CodePlex filed under the October 16th release of ASP.NET MVC futures. So is it looks like this is something I am doing wrong, but what?
Jason Whitehorn
A: 

Ok, I figured out what my problem was.

Apparently I was not even setting the ViewData slot that I was trying to read from in the view, resulting in it being a null value.

So effectually I was writing:

<%= Html.ActionLink<PhotoController>(p => p.Upload(null), "upload new photo") %>

I think the ultimate kicker to this whole thing was the fact that the parameter (groupname) represents a non-defaultable value in my routing table.

routes.MapRoute(
    "Group",  
    "group/{groupname}/{controller}/{action}/{id}",
    new {controller = "Photos", action = "View", Id = ""});

So according to the routing rule the property groupname has to be present, but according go the Lambda gropname was omitted (null). This resulted in the MVC framework being unable to find a route that satisfied my query, and just returning null.

Jason Whitehorn