Jenish Rabadiya Jenish Rabadiya - 1 year ago 107
jQuery Question

Running MVC application on IIS behaves differently than on running Visual studio localhost

I have mvc application deployed as application under one website in IIS.


public ActionResult Index()
List<SelectListItem> ddlCountry = countryService.GetCountries().Select(d => new SelectListItem() { Text = d.Name, Value = d.Id.ToString() }).ToList();
ddlCountry.Insert(0, new SelectListItem() { Text = string.Empty, Value = string.Empty });
ViewData["ddlCountry"] = ddlCountry;

public ActionResult GetCities(int Id)
if (!Convert.ToBoolean(ConfigurationManager.AppSettings["InDev"]))
return Redirect("");
var list = cityService.GetCities(Id).Select(d => new {Id = d.Id, Name = d.Name }).ToList();
return Json(list, JsonRequestBehavior.AllowGet);


<td>@Html.LabelFor(model => model.CountryId, "Country Name")</td>
<td>@Html.DropDownListFor(model => model.CountryId, ViewData["ddlCountry"] as List<SelectListItem>)</td>
<td>@Html.ValidationMessageFor(model => model.CountryId)</td>

<td>@Html.LabelFor(model => model.CityId, "City Name")</td>
<td>@Html.DropDownListFor(model => model.CityId, ViewData["ddlCity"] as List<SelectListItem>)</td>
<td>@Html.ValidationMessageFor(model => model.CityId)</td>


$(document).ready(function () {

$('#CountryId').change(function () {
loadDropDown("CityId", "BookingRequest/GetCities/" + $('#CountryId').val())

function loadDropDown(destinationId, Url) {
$.getJSON(Url, function (data) {
$('#' + destinationId).html($('<option></option>').val("").html(""));
$.each(data, function (index, item) {
$('#' + destinationId).append(

When I deployed it to IIS as an application under website it loads the cities properly. URL was like
. and When I change the country dropdown value it executes get on
and all things are working fine.

but when I hit run on the visual studio it and change the country it executes getCities on wrong URL:

My question is why this behaves differently on the localhost running from the visual studio and on the IIS? Is it due to the deployment as web application inside the website at IIS? Or is it anything to do with my browser?

Here is snap of that request from web developer tool of IE.

enter image description here

Answer Source

To get the correct relative path in MVC below are the following thing we need to do.

In MVC View :

var strUrl = '@Url.Action("actionName", "controllerName")';

In Javascript:

Get that variable "strUrl" and use in your ajax code.

 type: "GET",
 url: strUrl ,
 contentType: "application/json; charset=utf-8",
 dataType: "html",
 success: function (data) {