سؤال

I have the following Script inside my razor view:-

var idRack = $("#Firewall_RackID").val();
        $.getJSON("@Url.Content("~/Firewall/LoadDataCenterANDZoneByRack")", { id: idRack },
        function (RackData) {
            var select = $("#Firewall_TMSRack_DataCenter_Name");

which calls the following method:-

  public JsonResult LoadDataCenterANDZoneByRack(int id)
        {
            var rack = repository.AllIncludingRack_single(id,a=>a.DataCenter,a=>a.Zone);
            string datacentername = rack.DataCenter.Name;
            string zonename = rack.Zone.Name;

            var Data = new { DCValue = datacentername, ZValue = zonename };

            return Json(Data, JsonRequestBehavior.AllowGet);
        }

the problem i am facing is that in case the Firewall_RackID drop down is not selected (empty string) , i will get the following error , when i check the scripts using firebug tools:-

500 Internal Server Error

so can anyone adivce , how i can prevent the getJSON , from being called incase the var idRack is null or empty ? Thanks

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

المحلول

An if statement?

var idRack = $("#Firewall_RackID").val();
if (idRack != null || idRack != "") {
    //AJAX
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top