C# MVC 传统方法实现功能

Index.cshtml

@{
    Layout = null;
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        function DoDelete(StudentID) {
            $.post('@Url.Action("DoDeleteStudent", "Home")', { StudentID: StudentID }, function (result) {
                alert(result.Msg);
            });
        }
    </script>
</head>
<body>
    <div>
        <a href="@Url.Action("AddStudent","Home")">添加</a>
        <table>
            <tr>
                <th>编号</th>
                <th>名称</th>
            </tr>
            @*遍历学校信息*@
            @foreach (var school in Model.Schools)
            {
                <tr>
                    <td>@school.SchoolID</td>
                    <td>@school.SchoolName</td>
                </tr>
            }
        </table>
        <table>
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>操作</th>
            </tr>
            @*遍历学生信息*@
            @foreach (var student in Model.Students)
            {
                <tr>
                    <td>@student.StudentID</td>
                    <td>@student.StudentName</td>
                    <td><a href="javascript:void(0);" οnclick="DoDelete(@student.StudentID)">删除</a></td>
                </tr>
            }
        </table>
    </div>
</body>
</html>

AddStudent.cshtml

@model WebApplication9.Controllers.Student
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AddStudent</title>
    <!--验证必须添加js-->
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
</head>
<body>
    <div>
        @using (Html.BeginForm("DoAddStudent", "Home", FormMethod.Post, new { @class = "MyForm" }))
        {
            @Html.ValidationSummary(true)
            <table>
                <tr>
                    <td>@Html.LabelFor(s => s.StudentID, new { @class = "MyLabel" })</td>
                    <td>
                        @Html.TextBoxFor(s => s.StudentID, new { @class = "MyTextBox" })
                        @Html.ValidationMessageFor(s=>s.StudentID)
                    </td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(s => s.StudentName, new { @class = "MyLabel" })</td>
                    <td>
                        @Html.TextBoxFor(s => s.StudentName, new { @class = "MyTextBox" })
                        @Html.ValidationMessageFor(s => s.StudentName)
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" value="提交" />
                    </td>
                </tr>
            </table>
        }
    </div>
</body>
</html>

HomeController

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

namespace WebApplication9.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Test test = GetTest();
            return View(test);//MVC返回多个对象
        }
        public ActionResult AddStudent()
        {
            return View();
        }
        public ActionResult DoAddStudent(Student student)
        {
            Test test = GetTest();
            return View("Index", test);
        }
        public JsonResult DoDeleteStudent(int StudentID)
        {
            Message message = new Message { Success = 1, Msg = "删除成功" };
            return Json(message);
        }
        private Test GetTest()
        {
            List<Student> Students = new List<Student>();
            Students.Add(new Student { StudentID = 1, StudentName = "张三" });
            Students.Add(new Student { StudentID = 2, StudentName = "李四" });
            Students.Add(new Student { StudentID = 3, StudentName = "王五" });
            List<School> Schools = new List<School>();
            Schools.Add(new School { SchoolID = 1, SchoolName = "清华大学" });
            Schools.Add(new School { SchoolID = 2, SchoolName = "北京大学" });
            Schools.Add(new School { SchoolID = 3, SchoolName = "浙江大学" });
            Test test = new Test { Schools = Schools, Students = Students };
            return test;
        }
    }
    #region 测试对象
    public class Message
    {
        public int Success { get; set; }
        public string Msg { get; set; }
    }
    public class Test
    {
        public List<School> Schools { get; set; }
        public List<Student> Students { get; set; }
    }
    public class Student
    {
        //显示和必填字段
        [DisplayName("学号")]
        [Required(ErrorMessage = "请填写学号")]
        public int StudentID { get; set; }
        [DisplayName("学生姓名")]
        [Required(ErrorMessage = "请填写学生姓名")]
        public string StudentName { get; set; }
    }
    public class School
    {
        public int SchoolID { get; set; }
        public string SchoolName { get; set; }
    }
    #endregion
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值