MVC——数据库增删改查(Razor)——Html语法

一、显示界面
1、Models(模板)
 private MyDBDataContext _context = new MyDBDataContext();
        public List<Info> Select() //查询
        {
            return _context.Info.ToList();
        }
2、Contrallers(控制器)
    public ActionResult Index()
        {
            //在这里没用ViewBag
            List<Info> list = new InfoBF().Select();
            return View(list); //强制类型
        }
3、Views(视图)
@using MVC__数据库增删改查_Razor__Html语法.Models;
@*声明一下强制数据的类型↓*@
@model List<Info>     
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style>
        .trhead 
        {
            background-color:#b200ff
            
        }
        .trrow 
        { 
             background-color:#FFFFcc
        }
    </style>
</head>
<body>
    <div>
        @using(Html.BeginForm("Add","Home",FormMethod.Post))    表单
        {
            <input id="Submit1" type="submit" value="增加" />
        }
        <table width="100%" cellpadding="4" cellspacing="1" border="1">
            <tr class="trhead">
                <td>学号</td>
                <td>姓名</td>
                <td>操作</td>
               
            </tr>
            @{
                foreach (Info data in Model) //定义的强制类型,Model为List<Info>类型的
                {
               <tr class="trrow">
                
                   <td>@data.code</td>
                   <td>@data.name</td>         //超链接↓
                   <td>@Html.ActionLink("详细", "Details", "Home",new {id=data.code}, new { })&nbsp;&nbsp;@Html.ActionLink("删除", "Delete", "Home",new {id=data.code}, new { })&nbsp;&nbsp;@Html.ActionLink("修改", "Edit", "Home",new {id=data.code}, new { })&nbsp;&nbsp;</td>     
                </tr>
                }
          
            }
            </table>
    </div>
</body>
</html>

 效果图:

 

 

 

 

二、详细信息
1、Models(模板)
 public Info Select(string code)//详细查询
        {
            var query = _context.Info.Where(P=>P.code==code);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }
2、Controllers(控制器)
 public ActionResult Details(string id) //详细查询
        {
            Info data=new InfoBF().Select(id);//还是强类型
            return View(data);
        }
3、Views(视图)
@using MVC__数据库增删改查_Razor__Html语法.Models;
@model Info
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Details</title>
     <style>
        .trhead 
        {
            background-color:#b200ff
            
        }
        .trrow 
        { 
             background-color:#FFFFcc
        }
    </style>
</head>
<body>
    <div>
        <table width="100%" cellpadding="4" cellspacing="1" border="1">
            <tr class="trhead">
                <td>学号</td>
                <td>@Model.code</td>
            </tr>
              <tr class="trrow">
                <td>姓名</td>
                <td>@Model.name</td>
            </tr>
              <tr class="trrow">
                <td>性别</td>
                <td>@Model.sex</td>
            </tr>
              <tr class="trrow">
                <td>生日</td>
                <td>@Model.birthday</td>
            </tr>
              <tr class="trrow">
                <td>班级</td>
                <td>@Model.banji</td>
            </tr>
            </table>
    </div>
</body>
</html>

效果图:

 

 

 

 

三、增加
1、Models(模板)
public void Insert(string code,string  name,string sex,string birthday,string banji) //增加
        {
            Info data = new Info();
            data.code = code;
            data.name = name;
            data.sex = sex;
            data.birthday = birthday;
            data.banji = banji;
            _context.Info.InsertOnSubmit(data);
            _context.SubmitChanges();
        }
2、Controllers(控制器)
 public ActionResult Add() //增加页面
        {
            Info data = new Info(); //造一个对象用来存储数据
            data.sex = "";//默认选中性别 “男”
            List<banji> b = new banjiBF().Selcet(); //下拉列表
            ViewBag.bb = new SelectList(b,"banji1","banji1"); //banji1是表里的名字
            return View(data);//把这个对象送到视图里面去
        }
        public ActionResult Insert(string code,string name,string sex,string birthday,string banji)//增加按钮
        {
            new InfoBF().Insert(code,name,sex,birthday,banji);
            return RedirectToAction("Index");
        }
3、Views(视图)
@using MVC__数据库增删改查_Razor__Html语法.Models;
@model Info
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("Insert", "Home", FormMethod.Post))
        {
             SelectList xx=ViewBag.bb;
            <div>
               @* 将数据送到Insert*@
                
                学号: @Html.TextBoxFor(P => P.code, new { maxlength=5})<br>
                姓名: @Html.TextBoxFor(P=>P.name)<br>
                性别: @Html.RadioButtonFor(P => P.sex, "男")男 @Html.RadioButtonFor(P=>P.sex,"女")女<br>
                生日: @Html.TextBoxFor(P=>P.birthday)<br>
               班级: @Html.DropDownListFor(P=>P.banji,xx)<br>
               
                <input id="Submit1" type="submit" value="增加" />
            </div>
        }
    </div>
</body>
</html>

 效果图:

      →

 

 

 

 

四、修改
1、Models(模板)
   public void Update(string code,string name,string sex,string birthday,string banji)
        {
            var query = _context.Info.Where(p => p.code == code);
            if(query.Count()>0)
            {
            Info x = query.First();
            x.code = code;
            x.name = name;
            x.sex = sex;
            x.birthday = birthday;
            x.banji = banji;
            }
            _context.SubmitChanges();
        }
2、Controllers(控制器)
 public ActionResult Edit(string id)
        {
            Info x =new InfoBF().Select(id);
            List<banji> b = new banjiBF().Selcet();//班级表
            ViewBag.bb = new SelectList(b, "banji1", "banji1"); //为了将下拉列表里的数据传到Edit里面去
            return View(x);
        
        }
        public ActionResult Update(string code,string name,string sex,string birthday,string banji)
        {
            new InfoBF().Update(code,name,sex,birthday,banji);
            return RedirectToAction("Index");

        }
3、Views(视图)
@using MVC__数据库增删改查_Razor__Html语法.Models;
@model Info
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>
    <div>
          @using (Html.BeginForm("Update", "Home", FormMethod.Post))
        {
             SelectList xx=ViewBag.bb;
            <div>
               @* 将数据送到Insert*@
                
                学号: @Html.TextBoxFor(P=>P.code,new { maxlength=5 } )<br>
                姓名: @Html.TextBoxFor(P=>P.name)<br>
                性别: @Html.RadioButtonFor(P => P.sex, "")男 @Html.RadioButtonFor(P=>P.sex,"")女<br>
                生日: @Html.TextBoxFor(P=>P.birthday)<br>
               班级: @Html.DropDownListFor(P=>P.banji,xx)<br>
               
                <input id="Submit1" type="submit" value="更新" />
            </div>
        }
    </div>
</body>
</html>

效果图:

 

 

 

 

五、删除
1、Models(模板)
   public void Delete(string code)//删除
        {
            var query = _context.Info.Where(P => P.code == code);
            if (query.Count() > 0)
            {
                Info a = query.First();
                _context.Info.DeleteOnSubmit(a);
                
            }
            _context.SubmitChanges();
        }
二、Controllers(控制器)
  public ActionResult Delete(string id)//删除按钮
        {
            new InfoBF().Delete(id);
            return RedirectToAction("Index");
        }
三、Views(视图)
null

效果图:

posted @ 2015-07-08 19:05  XCml  阅读(713)  评论(0编辑  收藏  举报