سؤال

I have asp.net mvc 4 project where try to implement localization following this article http://www.chambaud.com/2013/02/27/localization-in-asp-net-mvc-4/ everything work fine when I try to use normal links, but when I try to use ajax links, in next example I have always error:

$.ajax({
            url: '/Home/GetJobList',
            contentType: 'application/html; charset=utf-8',
            type: 'GET',
            dataType: 'html'

        })
        .success(function (result) {
            $('#jobsTable').html(result);
        })
        .error(function (xhr, status) {
            alert(status);
        });

And in this example my ajax request isn't sending to the server:

$.ajax({
            url: "/" + $.cookie("lang") + '/Home/GetJobList',
            contentType: 'application/html; charset=utf-8',
            type: 'GET',
            dataType: 'html'

        })
        .success(function (result) {
            $('#jobsTable').html(result);
        })
        .error(function (xhr, status) {
            alert(status);
        });

Error what I have is inside the code in Global.asax.cs:

var handler = Context.Handler as MvcHandler;
var routeData = handler != null ? handler.RequestContext.RouteData : null;
var routeCulture = routeData != null ? routeData.Values["culture"].ToString() : null; //here is an error

I'm try to debugging and found that it's have only Controller, Action, index and dont receive culture:

routes.MapRoute(
    name: "Default",
    url: "{culture}/{controller}/{action}/{id}",
    defaults: new { culture = "ru", controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Can anybody help me?

هل كانت مفيدة؟

المحلول

If you use razor you could write

    url : '@Url.Action("GetJobList", "Home")',

The razor helper then automatically translates '@Url.Action("ControllerMethod", "Controller")' into the set url pattern of your asp.net mvc project.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top