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>

时间: 2024-08-04 04:36:07

MVC Razor的相关文章

Asp.Net Mvc Razor视图语法

    Asp.Net Mvc Razor视图语法 [email protected]符号输出变量值 2.使用C#语法嵌套Html标签循环输出NameList的值:使用@{C#语法代码}: 3.输出@符号使用两个@@ 4.服务器注释使用@*注释内容*@ [email protected]:输出文本,或者使用<text></text> 6.在@()括号进行简单的操作或运算 [email protected]()或@{}可以输出带有html标签的字符串 8.Razor可以智能识别邮箱写

MVC Razor 语法(转)

http://blog.csdn.net/pasic/article/details/7072340 原文地址:MVC Razor 语法(转)作者:panzhaowen_jacki 语法名称 Razor 语法 Web Forms 等效语法 代码块 @{ int x = 123; string y = "because."; } <% int x = 123; string y = "because."; %> 表达式(默认encode) <span

体验 ASP.NET Core 1.1 中预编译 MVC Razor 视图

这是从 ASP.NET Core 1.1 官方发布博文中学到的一招,可以在 dontet publish 时将 Razor 视图编译为 .dll 文件. 需要在 project.json 中添加如下配置: 1)在 "dependencies" 中添加: "Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design": "1.1.0-preview4-final" 2)在"tools&quo

Asp.Net Core Mvc Razor之RazorPage

在AspNetCore.Mvc.Razor命名空间中的RazorPage继承RazorPageBase,并定义的属性为: HttpContext Context 表示当前请求执行的HttpContext RazorPageBase定义为抽象类,并继承了接口:IRazorPage IRazorPage接口定义属性如下: ViewContent ViewContent 获取或设置渲染视图的视图上下文 IHtmlContent BodyContent 获取或设置正文内容 bool IsLayoutBe

Asp.net MVC Razor常见问题及解决方法

没有经验的童鞋就是这样磕磕碰碰出来的经验. 1,Datatype的错误提示消息无法自定义 这也许是Asp.net MVC的一个Bug.ViewModel中定义了DataType为Date字段: [Required(ErrorMessage = "Birthday must be input!")] [DataType(DataType.Date, ErrorMessage = "Please enter a date like(2017-07-19).")] pub

ASP.NET MVC Razor

Razor是MVC3中才有的新的视图引擎.我们知道,在ASP.NET中,ASPX的视图引擎依靠<%和%>来调用C#指令.而MVC3以后有了一套新的使用@标记的Razor语法,使用起来更灵活更简洁.下面通过一些简单示例让大家快速撑握Razor语法的使用. 准备工作 在演示Razor语法的使用之前,我们需要做一些准备工作. 1.打开VS创建一个ASP.NET MVC空项目,很简单,就不具体演示了. 2.添加一个Model.在项目的Models文件夹中添加一个名为Product的类.在这我们把前一篇

ASP.NET MVC Razor视图(2)

昨天介绍了一些Razor的基本语法,几天接着向下说: 补成一个,上次介绍了怎么输出原样的文本,用<text></text>标签,下面再介绍一种语法: @{@:我爱北京}  这个@:我爱北京必须写在@{}C#代码段中,否则报错 1.输出@符号怎么做? 在Razor中,我们用@@输出"@",类似于C#中"//" 2.我们可以在Razor使用Response.Write(),Server,Request,Session,这是为什么呢? 我们反编译.

Asp.net MVC Razor常见问题及解决方法(转载&gt;云中客)

没有经验的童鞋就是这样磕磕碰碰出来的经验. 1,Datatype的错误提示消息无法自定义 这也许是Asp.net MVC的一个Bug.ViewModel中定义了DataType为Date字段: 1 2 3 [Required(ErrorMessage = "Birthday must be input!")] [DataType(DataType.Date, ErrorMessage = "Please enter a date like(2017-07-19)."

ASP.NET MVC Razor 输出没有编码的HTML字符串

Razor引擎之前要输出一段没有编码的字符串,只要@加变量名就可以了,Razor却不能这样,感觉是有点麻烦. 在Razor Beta 2以前的版本可以: @(new HtmlString(mystring)) 以后的版本可以 @Html.Raw(mystring) 在MVC 3中,你可以这样: ViewBag.Stuff = "<li>Menu</li>" //  在视图中可以这样输出 @MvcHtmlString.Create(ViewBag.Stuff) 相