I have a similar question about jQuery button click handler code not being fired at all here.
In this case, it is being fired (when the jQuery is added to a sta
Ok, this solution solves your problem but imply some changes in your code:
In the view, when you call Url.Action
, you're trying to generate a webapi route from a mvc controller. You can see this question where I say how to do this.
Basically the idea is using Url.RouteUrl
with an extra route value httproute = true
.
Now, you need to change some parts in your code to be able to use this:
Firstly, you are using attributes to define your web api routes, therefore this routes will be added after the routes defined in your WebApiConfig
class, and as I mencioned in the answer of the referenced question, Url.RouteUrl
will return the first route that match the route values. So, you need to declare the route in WebApiConfig
before the default route:
/* attributes removed */
public class LandingPageController : ApiController
{
public HttpResponseMessage GetQuadrantData(string unit, string begdate, string enddate)
{
...
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
/* Route added before the default one */
config.Routes.MapHttpRoute(
name: "QuadrantData",
routeTemplate: "api/{unit}/{begdate}/{enddate}"
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
If you don't do this, the default route will be matched since {id}
is optional and unit
, begdate
and enddate
will be passed as query params.
Once fixed this issue, you have another problem, to be able to use Url.RouteUrl
you need to know all the route values (unit, begdate, enddate), that are availables only when the script function is executed. But all razor code in your .cshtml is processed in the server when the html is generated for the client.
As you can see, when the javascript is executed you are not executing '@Url.RouteUrl...'
or '@Url.Action...'
, at this time you are just handling a string with value returned when the razor expression was processed.
One thing you can do is pass "template" values to the Url.RouteUrl
call, and replace it when the script is executed with the real values:
var url = '@Url.RouteUrl(new
{
unit = "(unit)",
begdate = "(begdate)",
enddate = "(enddate)",
httproute = true
})'
url = url.replace("(unit)", unitval)
.replace("(begdate)", begdateval)
.replace("(enddate)", enddateval)
$.ajax({
type: 'GET',
url: url,
contentType: 'application/json',
cache: false,
success: function (returneddata) {
},
error: function () {
alert('hey, boo-boo!');
}
});
After the replace
s the value of the variable url
will be like this /api/ABUELOS/2016-08-07/2016-08-13
.
If you don't want to move the routes to WebApiConfig
, you can set a name in RouteAttribute
:
[RoutePrefix("api")]
public class LandingPageController : ApiController
{
[Route("{unit}/{begdate}/{enddate}", Name = "QuadrantData")]
public HttpResponseMessage GetQuadrantData(string unit, string begdate, string enddate)
{
...
And specify the route name in Url.RouteUrl
call:
var url = '@Url.RouteUrl("QuadrantData", new
{
unit = "(unit)",
begdate = "(begdate)",
enddate = "(enddate)",
httproute = true
})'
This way is less intrusive in your code, but you need to know the name of the route you want to use.
Hope this helps.