C#MVC 기존 접근 방식 기능 구현

5332 단어 c#web
Index.cshtml
@{
    Layout = null;
}




    
    Index
    
    
        function DoDelete(StudentID) {
            $.post('@Url.Action("DoDeleteStudent", "Home")', { StudentID: StudentID }, function (result) {
                alert(result.Msg);
            });
        }
    


    
덧붙이다 @* *@ @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
}

좋은 웹페이지 즐겨찾기