2014-05-17 20 views
0

我已經包含JavaScript代碼數據提交到控制器asp.net Ajax的提交事件顯示從控制器返回的警報

數據在控制器成功處理的視圖,不過頁顯示,而不是JSON結果從控制器返回json結果而不是alert。任何想法 ?

這裏是我的代碼

<code> 
@using (@Html.BeginForm()) 
{ 
//some thing 

<input id="btSaveDetails" type="submit" name="btnSave" value="Save" /> 

} 

javascript 
<script type="text/javascript"> 
    $("#btSaveDetails").submit(function (e) { 
     $.ajax({ 
      type: "POST", 
      cache: false, 
      async: true, 
      url: '@Url.Action("Groups", "User")', 
      data: $('form').serialize(), 
      dataType: "json", 
      success: function(response) { 
       alert(response.Status); 
      }, 
      error: function (XMLHttpRequest, textStatus, errorThrown) { 
       if (errorThrown == "Forbidden") { 
        alert("Forbidden access"); 
       } else { 
        alert('An error occurred please retry.'); 
       } 
      } 
     }); 
    }); 

</script> 

controller : 
    public JsonResult Groups(GroupsModel userGroups) 
     { 
//some processing 
return Json(new { Status = "Saved" }); 
} 
</code> 

感謝

+0

嘗試'返回JSON(新{狀態= 「已保存」},JsonRequestBehavior.AllowGet);' –

+1

的表單被提交,沒有任何東西阻止默認提交 – adeneo

+1

嘗試將輸入類型更改爲「按鈕」而不是「提交」以防止默認表單提交或提交函數阻止默認事件,如e.preventDe錯誤()並返回false – malkam

回答

0

我改變了html.beginform幫手如下,

請試試這個,讓我知道。

<pre> <code><script src="~/Scripts/jquery-1.8.2.min.js"></script> @using (@Html.BeginForm("Groups", "User")) { 
<input type="text" /> <input id="btSaveDetails" type="submit" name="btnSave" value="Save" /> } <script type="text/javascript"> 
$("#btSaveDetails").submit(function (e) { 
    $.ajax({ 
     type: "POST", 
     cache: false, 
     async: true, 
     url: '@Url.Action("Groups", "User")', 
     data: $('form').serialize(), 
     dataType: "json", 
     success: function (response) { 
      alert(response.Status); 
     }, 
     error: function (XMLHttpRequest, textStatus, errorThrown) { 
      if (errorThrown == "Forbidden") { 
       alert("Forbidden access"); 
      } else { 
       alert('An error occurred please retry.'); 
      } 
     } 
    }); 
}); 

並且還修改了成功響應如下

How to convert a json object to a string in a alert box?

相關問題