I need to generate urls for use in javascript. I don't think my current solution is very readable.
//Navigation during load: Id from MVC or hash or zero.
var m_departmentId = <%= ViewContext.RouteData.Values["id"] ?? 0 %>;
if(0 == m_departmentId)
{
var hash = location.hash;
//Only one hash value
var id = parseInt(hash.substring(2));
//If it's a real Id, use it.
if (!isNaN(id)) {
m_departmentId = id;
}
}
var gridUrl = '<%= Url.Action("GetEmployees", "Company", new { area = "" }) %>/' + m_departmentId;
This script is placed at top of my page javascript where I translate aspnet-variables to js-variables.
Can I get rid of the first javascript?
Can I improve the readability of the last row? Imho it's rather hideous, especially within my page javascript.
Other improvements?
Improvement 1:
var gridUrl = '<%= Url.Content("~/Company/GetEmployees/") }) %>' + m_departmentId;
T4MVC didn't work when the action was the default action for the route, because the action was stripped from the path when no action argument was used.