添加显示分页修改反填MVC

本文介绍如何在ASP.NET MVC中实现分页功能,并在用户修改信息时反填当前数据。首先展示了控制器部分,包括Index、AddUser和UptUser三个方法,接着演示了前端视图的添加和显示,最后详细说明了修改信息时如何反填页面数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

1、控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebMVC2.Controllers
{
public class UserController : Controller
{
// GET: User
public ActionResult Index()
{
return View();
}
public ActionResult AddUser()
{
return View();
}
public ActionResult UptUser(int id)
{
ViewBag.id = id;
return View();
}
}
}

2、添加前台

@{
Layout = null;
}

<meta name="viewport" content="width=device-width" />
<title>AddUser</title>
<script src="~/Scripts/jquery-3.4.1.js"></script>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script>
    $(function () {
        Bind();
    })
    function Add() {
        var obj = {
            UName: $("#name").val(),
            UAge: $("#age").val(),
            TId: $("#type").val()
        };
        $.ajax({
            url: "http://localhost:5824/api/User/AddUser",
            data: obj,
            type: "post",
            dataType: "json",
            success: function (d) {
                if (d > 0) {
                    alert("提交成功");
                    location.href = "/User/Index";
                }
                else {
                    alert("失败");
                }
            }
        })
    }
    //下拉
    function Bind() {
        $.ajax({
            url: "http://localhost:5824/api/User/BindType",
            type: "get",
            dataType: "json",
            success: function (d) {
                $("#type").empty();
                $(d).each(function () {
                    $("#type").append(
                        "<option value='" + this.TId + "'>" + this.TName + "</option>"
                    );
                })
            }
        })
    }
</script>


<div> 
    <table class="table">
        <tr>
            <td>姓名</td>
            <td>
                <input id="name" type="text" />
            </td>
        </tr>
        <tr>
            <td>类型</td>
            <td>
                <select id="type">
                </select>
            </td>
        </tr>
        <tr>
            <td>年龄</td>
            <td>
                <input id="age" type="text" />
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="提交" onclick="Add()" />
            </td>
        </tr>
    </table>
</div>

3、显示前台

@{
Layout = null;
}

<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-3.4.1.js"></script>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script>
    var pageindex = 1;
    var pagesize = 2;
    var pagecount = 1;
    $(function () {
        Show();
    })
    function Show() {
        $.ajax({
            url: "http://localhost:5824/api/User/GetUserInfos?pageindex=" + pageindex + "&&pagesize=" + pagesize,
            type: "get",
            dataType: "json",
            success: function (d) {
                pageindex = d.PageIndex;
                pagesize = d.PageSize;
                pagecount = d.PageCount;
                $("#pcount").text(d.PageCount);
                $("#acount").text(d.AllCount);
                $("#pindex").text(d.PageIndex);
                $("#tb").empty();
                $(d.List2).each(function () {
                    $("#tb").append(
                        "<tr>" +
                        "<td>" + this.UName + "</td>" +
                        "<td>" + this.TName + "</td>" +
                        "<td>" + this.UAge + "</td>" +
                        "<td>" + "<input id='Buttondel' type='button' value='删除' onclick='Del(" + this.UId + ")' />" + "</td>" +
                        "<td>" + "<input id='Buttonupt' type='button' value='修改' onclick='Upt(" + this.UId + ")' />" + "</td>" +
                        "</tr>"
                    );
                })
            }
        })
    }
    //删除
    function Del(id) {
        $.ajax({
            url: "http://localhost:5824/api/User/DelUser?id=" + id,
            type: "post",
            dataType: "json",
            success: function (d) {
                if (d > 0) {
                    alert("成功");
                    Show();
                }
                else {
                    alert("失败");
                }
            }
        })
    }
    function Upt(id) {
        location.href = "/User/UptUser?id=" + id;
    }
    function first() {
        pageindex = 1;
        Show();
    }
    function pre() {
        if (pageindex <= 1) {
            alert("没有上一页了");
        }
        else {
            pageindex--;
            Show();
        }
    }
    function next() {
        if (pageindex >= pagecount) {
            alert("没有下一页了");
        }
        else {
            pageindex++;
            Show();
        }
    }
    function last() {
        pageindex = pagecount;
        Show();
    }
</script>


<div>
    <input id="Buttontiao" type="button" value="跳转添加" onclick="location.href='/User/AddUser'" />
    <table class="table">
        <tr>
            <td>姓名</td>
            <td>类型</td>
            <td>性别</td>
            <td>操作</td>
        </tr>
        <tbody id="tb"></tbody>
    </table>
    <p>共<span id="acount"></span>条数据 当前第<span id="pindex"></span>页 共<span id="pcount"></span>页</p>
    <input id="Buttonfirst" type="button" value="首页" onclick="first()" />
    <input id="Buttonpre" type="button" value="上一页" onclick="pre()" />
    <input id="Buttonnext" type="button" value="下一页" onclick="next()" />
    <input id="Buttonlast" type="button" value="尾页" onclick="last()" />
</div>

4、修改反填前台

@{
Layout = null;
}

<meta name="viewport" content="width=device-width" />
<title>UptUser</title>
<script src="~/Scripts/jquery-3.4.1.js"></script>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script>
    $(function () {
        Bind();
        Fan();
    })
    function Bind() {
        $.ajax({
            url: "http://localhost:5824/api/User/BindType",
            type: "get",
            dataType: "json",
            success: function (d) {
                $("#type").empty();
                $(d).each(function () {
                    $("#type").append(
                        "<option value='" + this.TId + "'>" + this.TName + "</option>"
                    );
                })
            }
        })
    }
    function Fan() {
        $.ajax({
            url: "http://localhost:5824/api/User/Fan?id=" + $("#Hidden1").val(),
            type: "get",
            dataType: "json",
            success: function (d) {
                $("#name").val(d.UName);
                $("#age").val(d.UAge);
                $("#type").val(d.TId);
            }
        })
    }
    function Upt() {
        var obj = {
            UName: $("#name").val(),
            UAge: $("#age").val(),
            TId: $("#type").val(),
            UId: $("#Hidden1").val()
        };
        $.ajax({
            url: "http://localhost:5824/api/User/UptUser",
            data: obj,
            type: "post",
            dataType: "json",
            success: function (d) {
                if (d > 0) {
                    alert("成功");
                    location.href = "/User/Index";
                }
                else {
                    alert("失败");
                }
            }
        })
    }
</script>


<div>
    <table class="table">
        <tr>
            <td>姓名</td>
            <td>
                <input id="name" type="text" />
                <input id="Hidden1" type="hidden" value="@ViewBag.id" />
            </td>
        </tr>
        <tr>
            <td>类型</td>
            <td>
                <select id="type">
                </select>
            </td>
        </tr>
        <tr>
            <td>年龄</td>
            <td>
                <input id="age" type="text" />
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="修改" onclick="Upt()" />
            </td>
        </tr>
    </table>
</div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值