警報

2011-07-11 42 views
0

我有一個ActionLink的在我的masterlayout這樣的:警報

@Html.ActionLink("Order Your Free Report 1", "CheckValue", "Product", null,new { id = "checkExists" }) 

我有一個操作方法是這樣的:

public ActionResult CheckValue() { 
      bool result = true; 
      ViewData["checkCondition"] = true; 
      return Json(result, JsonRequestBehavior.AllowGet); 
     } 

和功能類似這:

$(function() { 
    $('#checkExists').click(function() { 
    $.getJSON(this.href, function (result) { 
      alert(result); 
      if (result) { 
       alert('the record exists'); 
      }   
     }); 
     return false; 
    }); 
}); 

當我點擊鏈接,警告不顯示。但如果我這樣使用:

$(function() { 
    $('#checkExists').click(function() { 

     var condition =new Boolean('@ViewData["checkCondition"]'); 
     if (condition) { 
      alert("message"); 
     } 
    return false; 
    }); 
}); 

它的工作原理。請提出爲什麼第一個不工作?

+0

你用類似螢火蟲或其他瀏覽器的調試器,看看是否HTTP請求甚至正在被創建?是否有任何地方報告錯誤? – Pointy

+0

使用螢火蟲檢查你鏈接的'href'屬性的值是多少? – Rafay

+0

@ 3nigma:鏈接看起來像這樣在firebug html窗口中:Order Your Free Report 1 DotnetSparrow

回答

0

嘗試包裝this$(this)

$(function() { 
    $('#checkExists').click(function() { 
    $.getJSON($(this).attr('href'), function (result) { 
      alert(result); 
      if (result) { 
       alert('the record exists'); 
      }   
     }); 
     return false; 
    }); 
}); 

的AJAX版本

$(function() { 
     $('#checkExists').click(function() { 

     $.ajax({ 
     url: $(this).attr('href'), 
     type:'GET', 
     success: function (result) { ... }, 
     dataType: 'json', 
     error:function(jqXhr,textStatus, errorThrown){ 
      alert("Oops "); 
      alert(jqXhr.responseText); 
      alert(jqXhr.status); 
     } 
      }); 
      return false; 
     }); 
    }); 

試試這個,告訴什麼警報顯示

+0

3nigma:我嘗試了上面的代碼,但沒有顯示任何提醒。 – DotnetSparrow

+0

嘗試更新的答案 – Rafay