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
}