C# MVC 传统方法实现功能

Index.cshtml

@{
    Layout = null;
}




    
    Index
    
    


    
添加 @*遍历学校信息*@ @foreach (var school in Model.Schools) { }
编号 名称
@school.SchoolID @school.SchoolName
@*遍历学生信息*@ @foreach (var student in Model.Students) { }
编号 姓名 操作
@student.StudentID @student.StudentName 删除

AddStudent.cshtml

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





    
    AddStudent
    
    
    
    


    
@using (Html.BeginForm("DoAddStudent", "Home", FormMethod.Post, new { @class = "MyForm" })) { @Html.ValidationSummary(true)
@Html.LabelFor(s => s.StudentID, new { @class = "MyLabel" }) @Html.TextBoxFor(s => s.StudentID, new { @class = "MyTextBox" }) @Html.ValidationMessageFor(s=>s.StudentID)
@Html.LabelFor(s => s.StudentName, new { @class = "MyLabel" }) @Html.TextBoxFor(s => s.StudentName, new { @class = "MyTextBox" }) @Html.ValidationMessageFor(s => s.StudentName)
}

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 Students = new List();
            Students.Add(new Student { StudentID = 1, StudentName = "张三" });
            Students.Add(new Student { StudentID = 2, StudentName = "李四" });
            Students.Add(new Student { StudentID = 3, StudentName = "王五" });
            List Schools = new List();
            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 Schools { get; set; }
        public List 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
}


你可能感兴趣的:(c#,web)