本文实例讲述了JQuery异步加载PartialView的方法。分享给大家供大家参考,具体如下:
需求:页面上有dropdown之类的控件,当选择里面不同值的时候,下面关联的内容跟着改变。
思路:把与 dropdown关联的会改变的内容放到PartialView(ascx)里,用JQuery绑定dropdown的change事件,当选择值改变时,用JQuery ajax请求与PartialView相关的Action,得到数据后讲取到的内容覆盖原来的内容。
实现:
Model 类:
public class User
{
public string UserName { get; set; }
public int Age { get; set; }
public int UserID { get; set; }
public static List<User> GetUsers()
{
List<User> userList = new List<User>();
User user = null;
user = new User();
user.UserID = 1;
user.UserName = "小明";
user.Age = 20;
userList.Add(user);
user = new User();
user.UserID = 2;
user.UserName = "小红";
user.Age = 21;
userList.Add(user);
user = new User();
user.UserID = 3;
user.UserName = "小强";
user.Age = 22;
userList.Add(user);
return userList;
}
public static User GetUserById(int userId)
{
return GetUsers().SingleOrDefault(u=>u.UserID==userId);
}
}
我们的PartialView:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication2.Models.User>" %>
<div>
<%if (Model != null)
{%>
用户名:<%=Model.UserName%><br />
年龄:<%=Model.Age%>
<%} %>
</div>
主页面:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication2.Models.User>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<%=Html.DropDownList("users", ViewData["users"] as List<SelectListItem>)%>
<div id="userDetails">
<%Html.RenderPartial("UserDetails", Model); %>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadMeta" runat="server">
<script language="javascript" src="/UploadFiles/2021-04-02/user.js">
Controller类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class UserController : Controller
{
public ActionResult Index()
{
List<SelectListItem> userIdList = new List<SelectListItem>();
foreach (MvcApplication2.Models.User item in MvcApplication2.Models.User.GetUsers())
{
userIdList.Add(new SelectListItem { Text = item.UserName,Value = item.UserID.ToString()});
}
ViewData["users"] = userIdList;
MvcApplication2.Models.User user = MvcApplication2.Models.User.GetUsers().FirstOrDefault();
return View(user);
}
public PartialViewResult UserDetails(int"htmlcode">
<script language="javascript" type="text/javascript">
mySite = {
actions : {
userDetails: '<%=Url.Action("UserDetails","User")%>'
}
};
</script>
我们对应的JS代码:
$(document).ready(function () {
$("#users").change(function () {
dropDownChange();
});
});
function dropDownChange() {
var userId = $("#users").val();
$.ajax({
type: "POST",
url: mySite.actions.userDetails,
data: { userId: userId },
success: function (data) {
$("#userDetails").html(data);
}
});
}
这样就实现了选择相应的user,显示对应的详细信息了。
只是一个简单的Demo,希望对需要此功能的人起到帮助作用。
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jquery中Ajax用法总结》、《jQuery表格(table)操作技巧汇总》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》
希望本文所述对大家jQuery程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。