Search code examples
asp.net-mvclocalizationglobalizationcascadingdropdown

How to use a Cascading dropdownlist with Globalization of Views in MVC3


I am making an asp.net mvc solution with globalization.

I have implemented globalization using resource files (Resources.fr.resx & Resources.nl.resx) and

routes.MapRoute(
                    "Default",
                    "{language}/{controller}/{action}/{id}",
                    new { language="Nl", controller = "Home", action = "Index", id = UrlParameter.Optional },
                    new { language ="Nl|Fr" }
                );

Now i want to make 2 cascading dropdownlists, with something like this:

$('#ddl_Author').change(function () {
    var ddlsource = "#ddl_Author";
    var ddltarget = "#ddl_Books";

    $.getJSON('@Url.Action("Books")', { authorId: $(ddlsource).val() }, function (data) {
                $(ddltarget).empty();
                $.each(data, function (index, optionData) {
                    $(ddltarget).append("<option value='" + optionData.Value + "'>" + optionData.Text + "</option>");
                });
            });
});

My question is what i have to use as url, '@Url.Action("Books")' does not seem to work...

And this is my controller:

public ActionResult Books(int authorId)
{
     var books = _bookService.GetBooks(authorId);
     ...
     return Json(books.ToList(), JsonRequestBehavior.AllowGet);
}

Solution

  • You need to specify the language as it is not an optional parameter in your route:

    @Url.Action("Books", new { language = "Fr" })