MVC Razor

视图引擎采用Razor写的增删改查

Controllers

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

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        
        //主页视图
        public ActionResult Index()
        {
            ViewBag.stu = new StudentDA().Select();
            return View();
        }

        //修改页面视图
        public ActionResult xiugai(string id)
        {
            ViewBag.data = new StudentDA().Select(id);
            return View();
        }

        //更新按钮动作
        public ActionResult Update(string sno, string sname, string ssex, DateTime sbirthday, string class1)
        {
            try
            {
                new StudentDA().Update(sno, sname, ssex, sbirthday, class1);
                return RedirectToAction("Index");
            }
            catch (Exception)
            {

                return RedirectToAction("chucuo", "Home");
            }
        }

        //删除动作
        public ActionResult Delete(string id)
        {
            try
            {
                new StudentDA().Delete(id);
                return RedirectToAction("Index");
            }
            catch (Exception)
            {

                return RedirectToAction("Decuowu", "Home");
            }
        }

        //增加页面视图
        public ActionResult zengjia()
        {
            return View();
        }

        //添加按钮动作
        public ActionResult ADD(string sno, string sname, string ssex, DateTime sbirthday, string class1)
        {
            try
            {
                new StudentDA().Insert(sno, sname, ssex, sbirthday, class1);
                return RedirectToAction("Index");
            }
            catch (Exception)
            {

                return RedirectToAction("chucuo","Home");
            }
        }

        //错误页面视图
        public ActionResult chucuo()
        {
            return View();
        }

        //删除错误页面视图
        public ActionResult Decuowu()
        {
            return View();
        }
        //回到主页按钮动作
        public ActionResult tiaozhuan()
        {
            return RedirectToAction("Index");
        }
    }
}

Models

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
    public class StudentDA
    {
        private MyDbDataContext Context = new MyDbDataContext();

        //查询学生信息
        public List<student> Select()
        {
             return Context.student.ToList();
        }

        //按学号查询单个学生信息
        public student Select(string sno)
        {
            var query = Context.student.Where(p=>p.Sno==sno);
            if (query.Count()>0)
            {
                return query.First();
            }
            return null;
        }

        //插入数据库
        public void Insert(string sno,string sname,string ssex,DateTime sbirthday,string class1)
        {
            student data = new student 
            {
            Sno=sno,
            Sname=sname,
            Ssex=ssex,
            Sbirthday=sbirthday,
            Class=class1
            };
            Context.student.InsertOnSubmit(data);
            Context.SubmitChanges();
        }

        //删除
        public void Delete(string sno)
        {
            var query = Context.student.Where(p=>p.Sno==sno);
            if (query.Count()>0)
            {
                student stu = query.First();
                Context.student.DeleteOnSubmit(stu);
                Context.SubmitChanges();
            }
        }

        //修改
        public void Update(string sno, string sname, string ssex, DateTime sbirthday, string class1)
        {
            var query = Context.student.Where(p => p.Sno == sno);
            if (query.Count() > 0)
            {
                student stu = query.First();
                stu.Sname = sname;
                stu.Ssex = ssex;
                stu.Sbirthday = sbirthday;
                stu.Class = class1;
                Context.SubmitChanges();
            }
        }
    }
}

Views

  Index

@using MvcApplication1.Models;
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">

        .tbhead {
            background-color:navy;
            text-align:center;
            color:white;
            font-weight:bold;
        }
        .tbrow1 {
            text-align:center;
            background-color:#FFFFcc;
        }
         .tbrow2 {
            text-align:center;
            background-color:#ccFFFF;
        }
    </style>

</head>
<body>
    <div>
        <table id="tbstu" width="100%" cellpadding="4" cellspacing="1" border="0">
            <tr class="tbhead">
                <td>学号</td>
                <td>姓名</td>
                <td>性别</td>
                <td>生日</td>
                <td>班级</td>
                <td>操作</td>
            </tr>

            @{
                List<student> stu=ViewBag.stu as List<student>;
                int i=0;
                foreach(student s in stu)
                {                   
                    if(i%2==0)
                    {
            <tr class="tbrow1">
                <td>@s.Sno</td>
                <td>@s.Sname</td>
                <td>@s.Ssex</td>
                <td>@s.Sbirthday.Value.ToLongDateString().ToString()</td>
                <td>@s.Class</td>
                <td>
                    <a href="/Home/xiugai/@s.Sno">修改</a>
                    <a href="/Home/Delete/@s.Sno" onclick="returen confirm('确认删除 @s.Sname 吗?')">删除</a>
                </td>
            </tr>
            }
            else
            {
             <tr class="tbrow2">
                <td>@s.Sno</td>
                <td>@s.Sname</td>
                <td>@s.Ssex</td>
                <td>@s.Sbirthday.Value.ToLongDateString().ToString()</td>
                <td>@s.Class</td>
                <td>
                    <a href="/Home/xiugai/@s.Sno">修改</a>
                    <a href="/Home/Delete/@s.Sno" onclick="return confirm('确认删除 @s.Sname 吗?')">删除</a>
                </td>
              </tr>
                    }
                    i++;
            }
            }                
        </table>
        <form action="/Home/zengjia" method="post">
            <input type="submit" value="添加" />
        </form>
    </div>
</body>
</html>

zengjia

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>zengjia</title>
</head>
<body>
    <div>
        <h1>添加学生信息</h1>
        <form id="f1" name="f1" action="/Home/ADD" method="post">
           
            <ul class="uu">
                <li>学号:<input type="text" name="sno" /></li>
                <li>姓名:<input type="text" name="sname" /></li>
                <li>性别:<input type="text" name="ssex" /></li>
                <li>生日:<input type="text" name="sbirthday" /></li>
                <li>班级:<input type="text" name="class1" /></li>
            </ul>
                <input type="submit" name="submit" value="添加" />
            </form>
            <form action="/Home/tiaozhuan" method="post">
                <input type="submit" value="回到主页" />
            </form>        
    </div>
</body>
</html>

xiugai

@using MvcApplication1.Models;
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>xiugai</title>
    <style type="text/css">
        .uu {
        list-style:none;
        }
    </style>
</head>
<body>
    <div>
        <form id="f1" name="f1" action="/Home/Update" method="post">
            @{
                student s=ViewBag.data as student;
            <ul class="uu">
                <li>学号:<input type="text" name="sno" readonly="readonly" value="@s.Sno" /></li>
                <li>姓名:<input type="text" name="sname"  value="@s.Sname" /></li>
                <li>性别:<input type="text" name="ssex"  value="@s.Ssex" /></li>
                <li>生日:<input type="text" name="sbirthday"  value="@s.Sbirthday" /></li>
                <li>班级:<input type="text" name="class1"  value="@s.Class" /></li>
            </ul>
                <input type="submit" name="submit" value="更新" />
            }
         </form>
            <form action="/Home/tiaozhuan" method="post">
                <input type="submit" value="回到主页" />
            </form>
        
    </div>
</body>
</html>

Decuowu

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Decuowu</title>
</head>
<body>
    <div>
        <h1>此信息不能删除,受外键表约束</h1>
        <form action="/Home/tiaozhuan" method="post">
            <input type="submit" value="回到主页" />
        </form>
    </div>
</body>
</html>

chucuo

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>chucuo</title>
</head>
<body>
    <div>
        <h1>出错了!</h1>
        <form action="/Home/tiaozhuan" method="post">
            <input type="submit" value="回到主页" />
        </form>
    </div>
</body>
</html>

原文地址:https://www.cnblogs.com/happinesshappy/p/4625726.html