MVC 使用Jquery实现AJax

Myth丶恋晨 2022-09-17 07:28 277阅读 0赞

在上一篇介绍MVC中的Ajax实现方法的时候,曾经提到了除了使用Ajax HTML Helper方式来实现之外,Jquery也是实现Ajax的另外一种方案。

通过get方法实现AJax请求

View

  1. <script type="text/javascript">
  2. function GetTime() {
  3. $.get("Home/GetTime", function (response) {
  4. $("#myPnl").html(response);
  5. });
  6. return false;
  7. }
  8. </script>
  9. <div id="myPnl" style="width: 300px; height: 30px; border: 1px dotted silver;">
  10. </div>
  11. <a href="#" onclick="return GetTime();">Click Me</a>

Controller

  1. public ActionResult GetTime()
  2. {
  3. return Content(DateTime.Now.ToString());
  4. }

通过post方法实现Form的Ajax提交

View

  1. @model MvcAjax.Models.UserModel
  2. @{
  3. ViewBag.Title = "AjaxForm";
  4. }
  5. <script type="text/javascript">
  6. $(document).ready(function () {
  7. $("form[action$='SaveUser']").submit(function () {
  8. $.post($(this).attr("action"), $(this).serialize(), function (response) {
  9. $("#myPnl").html(response);
  10. });
  11. return false;
  12. });
  13. });
  14. </script>
  15. <div id="myPnl" style="width: 300px; height: 30px; border: 1px dotted silver;">
  16. </div>
  17. @using (Html.BeginForm("SaveUser", "Home"))
  18. {
  19. <table>
  20. <tr>
  21. <td>
  22. @Html.LabelFor(m => m.UserName)
  23. </td>
  24. <td>
  25. @Html.TextBoxFor(m => m.UserName)
  26. </td>
  27. </tr>
  28. <tr>
  29. <td>
  30. @Html.LabelFor(m => m.Email)
  31. </td>
  32. <td>
  33. @Html.TextBoxFor(m => m.Email)
  34. </td>
  35. </tr>
  36. <tr>
  37. <td>
  38. @Html.LabelFor(m => m.Desc)
  39. </td>
  40. <td>
  41. @Html.TextBoxFor(m => m.Desc)
  42. </td>
  43. </tr>
  44. <tr>
  45. <td colspan="2">
  46. <input type="submit" value="Submit" />
  47. </td>
  48. </tr>
  49. </table>
  50. }

Model

  1. using System.ComponentModel.DataAnnotations;
  2. namespace MvcAjax.Models
  3. {
  4. public class UserModel
  5. {
  6. [Display(Name = "Username")]
  7. public string UserName { get; set; }
  8. [Display(Name = "Email")]
  9. public string Email { get; set; }
  10. [Display(Name = "Description")]
  11. public string Desc { get; set; }
  12. }
  13. }

Controller

  1. [HttpPost]
  2. public ActionResult SaveUser(UserModel userModel)
  3. {
  4. //Save User Code Here
  5. //......
  6. return Content("Save Complete!");
  7. }

以上代码实现了Jquery POST提交数据的方法。

通过查看以上两种Jquery方式的Ajax实现方法,和之前AJax HTML Helper比较可以发现其实Controller都是相同的。

采用Jquery方式提交数据的的主要实现方案就是通过Jquery的get或者post方法,发送请求到MVC的controller中,然后处理获取的response,更新到页面中。

注意点:

无论是使用超链接和form方式来提交请求,javascript的方法始终都有一个返回值false,用来防止超链接的跳转或者是form的实际提交。

这个地方就会出现一个疑问:

如果针对该页面禁止了Javascript脚本,那么该页面就是跳转或者是form就是提交,返回的ActionResult处理就会出现问题了。

这个时候就需要在Controller中判断该请求是否是Ajax请求,根据不同的情况返回不同的ActionResult:

  1. [HttpPost]
  2. public ActionResult SaveUser(UserModel userModel)
  3. {
  4. //Save User Code Here
  5. //......
  6. if (Request.IsAjaxRequest())
  7. return Content("Save Complete!");
  8. else
  9. return View();
  10. }

发表评论

表情:
评论列表 (有 0 条评论,277人围观)

还没有评论,来说两句吧...

相关阅读