Reputation: 12002
I am trying to get the url for a known route name inside of a razor template.
Here is my partial view
@model ScripterEngine.ViewModels.CampaignViewModel
<script type="text/javascript">
function logTime(stage, status, async){
var target = "@UrlHelper.RouteUrl("timetracker.clockin")";
var postData =
{
'campaign_id': @Model.id,
'agent_id': is_system_agentid.value,
'log_id': is_attr_calldata.tracker_id,
'stage_name': stage
};
if( status == 'out'){
target = "@UrlHelper.RouteUrl("timetracker.clockout")";
}
if( async !== false){
async = true;
}
$.ajax({
type: 'POST',
url: target,
data: postData,
async: async,
dataType: "json",
error: function( jqXHR, textStatus, errorThrown ){
alert('clock ' + status + ' failed!' + jqXHR.status );
},
success: function(data){
if(data && data.id && status != 'out'){
is_attr_calldata.tracker_id = data.id;
}
}
});
}
</script>
Here is my route mapping
//Timetracker - ClockIn
routes.MapRoute(
"timetracker.clockin",
"timetracker/clockin",
new { controller = "TimeTracker", action = "ClockIn" }
);
//Timetracker - ClockOut
routes.MapRoute(
"timetracker.clockout",
"timetracker/clockout",
new { controller = "TimeTracker", action = "ClockOut" }
);
However, I get a compilation error after I start the program and navigate to the route .
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'System.Web.Mvc.UrlHelper.RouteUrl(string)'
The error point at this line
var target = "@UrlHelper.RouteUrl("timetracker.clockin")";
How can I properly get the Url from a giving route name?
Upvotes: 4
Views: 2304
Reputation: 18649
The UrlHelper
is exposed on a view as @Url
- the code for WebViewPage
has:
public UrlHelper Url { get; set; }
Try:
var target = '@Url.RouteUrl("timetracker.clockin")';
Upvotes: 3