如果值为null,则阻止调用getJson

时间:2014-05-14 14:47:25

标签: javascript jquery razor

我的剃刀视图中有以下脚本: -

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

调用以下方法: -

  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);
        }

我面临的问题是,如果未选择Firewall_RackID下拉列表(空字符串),当我使用firebug工具检查脚本时,我将收到以下错误: -

  

500内部服务器错误

所以任何人都可以,如何阻止getJSON被调用,因为var idRack是null还是空? 感谢

1 个答案:

答案 0 :(得分:2)

if声明?

var idRack = $("#Firewall_RackID").val();
if (idRack != null || idRack != "") {
    //AJAX
}