Help with MVC ActionLink

  • Thread starter Thread starter Lars Zeb
  • Start date Start date
L

Lars Zeb

I cannot figure out how to pass a parameter to the Show method on the FliersController. Intellisense does not include
the "flier" variable declared in the foreach statement. If I type in "flier.ID" as the parameter, the compiler says it
cannot resolve symbol 'flier'.

As you can see, the compiler sees the symbol 'flier' in the code render block, but not in the lambda expression.

<% foreach (Flier flier in ViewData.Model) {%>
<tr>
<td> <%= flier.Description%> </td>
<td>
<%# Html.ActionLink<FliersController>(c => c.Show(flier.ID), "Details")%> //compiler error
</td>
<% }%>

In FliersController:
public ActionResult Show(Guid id) {
Flier flier = _flierRepository.Get(id);
return View(flier);
}

Thanks, Lars
 
Lars Zeb said:
I cannot figure out how to pass a parameter to the Show method on the
FliersController. Intellisense does not include
the "flier" variable declared in the foreach statement. If I type in
"flier.ID" as the parameter, the compiler says it
cannot resolve symbol 'flier'.

As you can see, the compiler sees the symbol 'flier' in the code render
block, but not in the lambda expression.

<% foreach (Flier flier in ViewData.Model) {%>
<tr>
<td> <%= flier.Description%> </td>
<td>
<%# Html.ActionLink<FliersController>(c =>
c.Show(flier.ID), "Details")%> //compiler error

I take # is a typo?
</td>
<% }%>

In FliersController:
public ActionResult Show(Guid id) {
Flier flier = _flierRepository.Get(id);
return View(flier);
}

I don't recognize this pattern of usage. I can't find an ActionLink<T>
extension method anywhere in the current release of MVC, is it something
you've added? Does the anonymous function it receives get called during the
loop iteration or is it defered to a later time?

There is only one flier variable which is captured by all instances of the
Lambda. Hence any execution of the Lambda outside of the loop will see a
null value for flier because when executed thats the value it ends up with
after the loop is complete.

Doesn't explain why you get a compile error though.
 
this is because you defined the actionlink as a binding expression
<%#...>. binding expressions are delegates called in the ondatabind
event, so they can not see variables declared in the foreach loop, which
will called from a different method.

this was probably a typo on your part.

-- bruce (sqlwork.com)
 
Thanks to you both for your help. Yes, Anthony, it's an extension:
Microsoft.Web.Mvc.LinkExtensions

And Bruce, you're kind in suggesting a typo. I lifted the code from another example, not really understanding that that
code was using binding.

Where did you find documentation on the binding expression (<%#)?
 
Thanks for answers from Anthony and Bruce.

Hi Lars,

Thanks for your post.

Documentation on the binding expression is at here: Data-Binding Expression
Syntax (http://msdn.microsoft.com/en-us/library/bda9bbfx.aspx) and
Data-Binding Expressions Overview
(http://msdn.microsoft.com/en-us/library/ms178366.aspx).

Data-binding expressions are resolved when the DataBind method of a control
or of the Page class is called. However, Code Declaration Blocks (<% %>) is
resolved at page or control's render method. They are running in different
context, so you cannot access 'flier'.

Instead, I think your intension here should be listing all the action links
in a table. You should still use code render block here:
<%= Html.ActionLink<FliersController>(c => c.Show(flier.ID), "Details")%>

As an example, I suggest you take a look of
http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part
-1.aspx.
It uses both ways (code render block and data-binding expression) to render
Html.ActionLink. Hope it helps.

Have a nice day.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
 
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Lars Zeb said:
Thanks to you both for your help. Yes, Anthony, it's an extension:
Microsoft.Web.Mvc.LinkExtensions

Is that an additional assembly you have fetched from elsewhere? I can't
find it anywhere in the latest MVC install? Are you using the latest MVC
install?
 
Back
Top