I have a view which is not being called when I step through the code I see it get to the action in the controller and return the view but it does not load the view it just stays on the current page.
When a button is clicked on a specific page an ajax post is made calling the next page this does not load.
My contoller action looks like this:
public ActionResult Comparison(int[] y)
{
CostModel.Getsuff(y);
return View(CostModel);
}
My route map looks like this:
routes.MapRoute(
name: "Compare",
url: "Category/Comparison/{*xxxx}",
defaults: new { Controller = "Category", Action = "Comparison" }
).RouteHandler
= new CategoriesRouteHandler();
public class CategoriesRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
IRouteHandler handler = new MvcRouteHandler();
var vals = requestContext.RouteData.Values;
var list = vals["xxxx"].ToString().Split(',');
int[] y= new int[list.Length];
for (int i=0; i < list.Length; i++)
int.TryParse(list[i].ToString(),out y[i]);
vals.Add("y", y);
return handler.GetHttpHandler(requestContext);
}
}
My ajax
$.ajax({
type: "POST",
url: '@Url.Action("Comparison", "Category", new { Categories = "PLACEHOLDER"})'.replace('PLACEHOLDER', arr),
dataType: "json",
traditional: true,
success: function(msg){alert(msg)}
} );
As I mentioned previously when I trace it, it performs the ajax call fine and hits the action on the controller it just does not launch the view. I thought it may have something to do with the fact that the area is not defined anywhere? So perhaps its throwing it out, can’t see anywhere in MVC 4 to specify different area, but I am new to MVC. Any help will be appreciated.
That’s the whole point of AJAX => stay on the same page. If you want to replace the current page don’t use any AJAX. Use a normal
Html.ActionLink.And if your controller action only returns a
PartialViewand you would like to refresh only a portion of your page, you could do that in thesuccesscallback:Also notice that I have removed
dataType: 'json'because your controller action is not returning any JSON. It’s returningtext/html.