• 学习MVC第一个增删修功能的本记


    1:首先是Model层(Model不仅仅是实体层,还有普通的三层BLL,DAL代码都写在这里的! public class BLLStuedent{}):

    using System.ComponentModel.DataAnnotations;   //这个是验证的空间类;四种不同的;

    namespace MvcLogin.Models
    {
        public class Student
        {
            [Required(ErrorMessage = "ID不能为空")]
            public int Id { get; set; }

            [Required(ErrorMessage = "姓名不能为空")]
            public string Name { get; set; }

            [RegularExpression("^[\u7537\u5973]+$", ErrorMessage = "性别只能输入“男”或者“女”")]
            public string Sex { get; set; }

            [Range(1, 120, ErrorMessage = "年龄必须在1-120之间")]
            public int Age { get; set; }
        }
    }

    2:控制器的代码:

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

    namespace MvcLogin.Controllers
    {
       
        public class StudentController : Controller
        {
            static List<Student> students = new List<Student>();
            //
            // GET: /Student/

            public ActionResult Index()
            {
                return View(students);
            }

            //
            // GET: /Student/Details/5

            public ActionResult Details(Student s)
            {
                return View(s);
            }

            //
            // GET: /Student/Create

            public ActionResult Create()
            {
                return View();
            }

            //
            // POST: /Student/Create

            [HttpPost]
            public ActionResult Create(Student s)
            {
                try
                {
                    // TODO: Add insert logic here
                    if (!ModelState.IsValid)
                    {
                        return View("Create", s);
                    }
                    students.Add(s);
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
           
            //
            // GET: /Student/Edit/5
     
            public ActionResult Edit(int id)
            {
                Student s = new Student();
                foreach (Student stu in students)
                {
                    if (stu.Id == id)
                    {
                        s.Id = stu.Id;
                        s.Name = stu.Name;
                        s.Sex = stu.Sex;
                        s.Age = stu.Age;
                    }
                }
                return View(s);
            }

            //
            // POST: /Student/Edit/5

            [HttpPost]
            public ActionResult Edit(Student s)
            {
                try
                {
                    // TODO: Add update logic here
                    if (!ModelState.IsValid)
                    {
                        return View("Edit", s);
                    }
                    foreach (Student stu in students)
                    {
                        if (stu.Id == s.Id)
                        {
                            stu.Id = s.Id;
                            stu.Name = s.Name;
                            stu.Sex = s.Sex;
                            stu.Age = s.Age;
                        }
                    }
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }

            //
            // GET: /Student/Delete/5
     
            public ActionResult Delete(int id)
            {
                Student s = new Student();
                foreach(Student stu in students)
                {
                    if (stu.Id == id)
                    {
                        s.Id = stu.Id;
                        s.Name = stu.Name;
                        s.Sex = stu.Sex;
                        s.Age = stu.Age;
                    }
                }
                return View(s);
            }

            //
            // POST: /Student/Delete/5

            [HttpPost]
            public ActionResult Delete(Student s)
            {
                try
                {
                    // TODO: Add delete logic here
                    foreach (Student stu in students)
                    {
                        if (stu.Id == s.Id)
                        {
                            students.Remove(stu);
                            break;
                        }
                    }
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
        }
    }

    3:视图的代码:

    列表页的代码:

        <table>
            <tr>
                <th></th>
                <th>
                    Id
                </th>
                <th>
                    Name
                </th>
                <th>
                    Sex
                </th>
                <th>
                    Age
                </th>
            </tr>

        <% foreach (var item in Model) { %>
       
            <tr>
                <td>
                    <%: Html.ActionLink("Edit", "Edit", new { id=item.Id }) %> |
                    <%: Html.ActionLink("Details", "Details", item)%> |    //这边要特别注意的
                    <%: Html.ActionLink("Delete", "Delete", new { id=item.Id })%>
                </td>
                <td>
                    <%: item.Id %>
                </td>
                <td>
                    <%: item.Name %>
                </td>
                <td>
                    <%: item.Sex %>
                </td>
                <td>
                    <%: item.Age %>
                </td>
            </tr>
       
        <% } %>

        </table>

        <p>
            <%: Html.ActionLink("Create New", "Create") %>
        </p>

        <br />

    增加的代码:

        <% using (Html.BeginForm()) {%>
            <%: Html.ValidationSummary(true) %>

            <fieldset>
                <legend>Fields</legend>
               
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Id) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Id) %>
                    <%: Html.ValidationMessageFor(model => model.Id) %>
                </div>
               
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Name) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Name) %>
                    <%: Html.ValidationMessageFor(model => model.Name) %>
                </div>
               
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Sex) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Sex) %>
                    <%: Html.ValidationMessageFor(model => model.Sex) %>
                </div>
               
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Age) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Age) %>
                    <%: Html.ValidationMessageFor(model => model.Age) %>
                </div>
               
                <p>
                    <input type="submit" value="Create" />
                </p>
            </fieldset>

        <% } %>

        <div>
            <%: Html.ActionLink("Back to List", "Index") %>
        </div>

    编辑的代码:

        <% using (Html.BeginForm()) {%>
            <%: Html.ValidationSummary(true) %>
           
            <fieldset>
                <legend>Fields</legend>
               
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Name) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Name) %>
                    <%: Html.ValidationMessageFor(model => model.Name) %>
                </div>
               
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Sex) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Sex) %>
                    <%: Html.ValidationMessageFor(model => model.Sex) %>
                </div>
               
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Age) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Age) %>
                    <%: Html.ValidationMessageFor(model => model.Age) %>
                </div>
               
                <p>
                    <input type="submit" value="Save" />
                </p>
            </fieldset>

        <% } %>

        <div>
            <%: Html.ActionLink("Back to List", "Index") %>
        </div>

  • 相关阅读:
    java获取服务器的ip和地址
    如何产生好的词向量
    谈谈评价指标中的宏平均和微平均
    在NLP中深度学习模型何时需要树形结构?
    Windows下MetaMap工具安装
    ML 感知机(Perceptrons)
    ML 神经网络 NeuralNetworks
    NLP-特征选择
    ML 逻辑回归 Logistic Regression
    ML 线性回归Linear Regression
  • 原文地址:https://www.cnblogs.com/wujy/p/2336278.html
Copyright © 2020-2023  润新知