ASP.NET MVC and using Url.Action in partial views

We're using Visual Studio 2010, ASP.NET MVC 2, .NET Framework 4.0 and ReSharper 5.1.1723.15.

We've setup some partial views which are used across pages where there is a need for similar functionality on many pages.  In some of those partial views we've tried to setup JavaScript variables to store the link to an action for later use by jQuery.  While this works as we would like we get a ReSharper saying "Cannot resolve action [action name]".  I understand that this warning is due to the partial view not having a controller behind it therefore ReSharper is not able to determine whether or not this action is valid.  Aside from disabling this validation check is there a way to make it so that ReSharper will not produce a warning if we include a Url.Action in a partial view with no backing controller?

Thanks,
Matt

4 comments
Comment actions Permalink

Matt Garing wrote:

Hi Matt!

We've setup some partial views which are used across pages where there is a need for similar functionality on many pages.  In some of those partial views we've tried to setup JavaScript variables to store the link to an action for later use by jQuery.  While this works as we would like we get a ReSharper saying "Cannot resolve action ".  I understand that this warning is due to the partial view not having a controller behind it therefore ReSharper is not able to determine whether or not this action is valid.  Aside from disabling this validation check is there a way to make it so that ReSharper will not produce a warning if we include a Url.Action in a partial view with no backing controller?


The situation is not clear to me. Is is controller and action explicitly specified in partial action?
If so, they should be both resolved.
Can you, please, provide small specific example, with controller, actions, views?

WBR, Slava Trenogin.

0
Comment actions Permalink

We have a partial view called ActionBar.ascx that is on every view.  Within the partial view we include a Url.Action("Action").  This syntax will make MVC look to the controller of the view for that action.  So say the view is called Person MVC will default to Person and look for the Action action.  Since the partial view is included in the Person view the controller will still be Person.  Using this syntax in a partial view is when ReShaper is saying it can't resolve the action name.  I understand why Resharper reports this but I was curious if there is a way around this.

0
Comment actions Permalink

Matt Garing wrote:

We have a partial view called ActionBar.ascx that is on every view.  Within the partial view we include a Url.Action("Action").  This syntax will make MVC look to the controller of the view for that action.  So say the view is called Person MVC will default to Person and look for the Action action.  Since the partial view is included in the Person view the controller will still be Person.  Using this syntax in a partial view is when ReShaper is saying it can't resolve the action name.  I understand why Resharper reports this but I was curious if there is a way around this.


Ok, got it.
Yep, that case can't be resolved in design time, only in runtime.
You can supress error highlighting in such places by special comments:
     <%-- ReSharper disable Asp.NotResolved --%>
     <%= Url.Action("Action") %>
     <%-- ReSharper restore Asp.NotResolved --%>

0

Please sign in to leave a comment.