当前位置:Gxlcms > JavaScript > JQuery异步加载PartialView的方法

JQuery异步加载PartialView的方法

时间:2021-07-01 10:21:17 帮助过:5人阅读

本文实例讲述了JQuery异步加载PartialView的方法。分享给大家供大家参考,具体如下:

需求:页面上有dropdown之类的控件,当选择里面不同值的时候,下面关联的内容跟着改变。

思路:把与 dropdown关联的会改变的内容放到PartialView(ascx)里,用JQuery绑定dropdown的change事件,当选择值改变时,用JQuery ajax请求与PartialView相关的Action,得到数据后讲取到的内容覆盖原来的内容。

实现:

Model 类:

  1. public class User
  2. {
  3. public string UserName { get; set; }
  4. public int Age { get; set; }
  5. public int UserID { get; set; }
  6. public static List<User> GetUsers()
  7. {
  8. List<User> userList = new List<User>();
  9. User user = null;
  10. user = new User();
  11. user.UserID = 1;
  12. user.UserName = "小明";
  13. user.Age = 20;
  14. userList.Add(user);
  15. user = new User();
  16. user.UserID = 2;
  17. user.UserName = "小红";
  18. user.Age = 21;
  19. userList.Add(user);
  20. user = new User();
  21. user.UserID = 3;
  22. user.UserName = "小强";
  23. user.Age = 22;
  24. userList.Add(user);
  25. return userList;
  26. }
  27. public static User GetUserById(int userId)
  28. {
  29. return GetUsers().SingleOrDefault(u=>u.UserID==userId);
  30. }
  31. }

我们的PartialView:

  1. <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication2.Models.User>" %>
  2. <div>
  3. <%if (Model != null)
  4. {%>
  5. 用户名:<%=Model.UserName%><br />
  6. 年龄:<%=Model.Age%>
  7. <%} %>
  8. </div>

主页面:

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.User>" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
  3. Index
  4. </asp:Content>
  5. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  6. <h2>Index</h2>
  7. <%=Html.DropDownList("users", ViewData["users"] as List<SelectListItem>)%>
  8. <div id="userDetails">
  9. <%Html.RenderPartial("UserDetails", Model); %>
  10. </div>
  11. </asp:Content>
  12. <asp:Content ID="Content3" ContentPlaceHolderID="HeadMeta" runat="server">
  13. <script language="javascript" src="/Scripts/user.js" type="text/javascript"></script>
  14. </asp:Content>

Controller类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using MvcApplication2.Models;
  7. namespace MvcApplication2.Controllers
  8. {
  9. public class UserController : Controller
  10. {
  11. public ActionResult Index()
  12. {
  13. List<SelectListItem> userIdList = new List<SelectListItem>();
  14. foreach (MvcApplication2.Models.User item in MvcApplication2.Models.User.GetUsers())
  15. {
  16. userIdList.Add(new SelectListItem { Text = item.UserName,Value = item.UserID.ToString()});
  17. }
  18. ViewData["users"] = userIdList;
  19. MvcApplication2.Models.User user = MvcApplication2.Models.User.GetUsers().FirstOrDefault();
  20. return View(user);
  21. }
  22. public PartialViewResult UserDetails(int? userId)
  23. {
  24. MvcApplication2.Models.User user = null;
  25. if (userId == null)
  26. {
  27. user = MvcApplication2.Models.User.GetUsers().FirstOrDefault();
  28. }
  29. else
  30. {
  31. user = MvcApplication2.Models.User.GetUserById(userId.Value);
  32. }
  33. return PartialView(user);
  34. }
  35. }
  36. }

我们需要在Master页里指定我们的PartialView对应的Action路径,这样实现:

在Head里加上如下代码:

  1. <script language="javascript" type="text/javascript">
  2. mySite = {
  3. actions : {
  4. userDetails: '<%=Url.Action("UserDetails","User")%>'
  5. }
  6. };
  7. </script>

我们对应的JS代码:

  1. $(document).ready(function () {
  2. $("#users").change(function () {
  3. dropDownChange();
  4. });
  5. });
  6. function dropDownChange() {
  7. var userId = $("#users").val();
  8. $.ajax({
  9. type: "POST",
  10. url: mySite.actions.userDetails,
  11. data: { userId: userId },
  12. success: function (data) {
  13. $("#userDetails").html(data);
  14. }
  15. });
  16. }

这样就实现了选择相应的user,显示对应的详细信息了。

只是一个简单的Demo,希望对需要此功能的人起到帮助作用。

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jquery中Ajax用法总结》、《jQuery表格(table)操作技巧汇总》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》

希望本文所述对大家jQuery程序设计有所帮助。

人气教程排行