web MVC程序开发增删改查参考代码。思路是面向对象思想,以及在HTML代码中嵌入C#代码

第一步:在Model中写好查询,删除,修改,添加方法,用sql_link类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



namespace MvcApplication2.Models

{



    public class InfoBf

    {

        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 sbirthdy,string @class)

        {

            student data = new student

            {

                sno = sno,

                sname = sname,

                ssex = ssex,

                sbirthdy = sbirthdy,

                @class = @class

            };

            _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 data = query.First();

                _Context.student.DeleteOnSubmit(data);

                _Context.SubmitChanges();

            }

        }

        //修改

        public void Update(string sno, string sname, string ssex, DateTime sbirthdy, string @class)

        {

            var query = _Context.student.Where(p => p.sno == sno);

            if (query.Count()>0)

            {

                student data = query.First();

                data.sno = sno;

                data.sname = sname;

                data.ssex = ssex;

                data.sbirthdy = sbirthdy;

                data.@class = @class;

                _Context.SubmitChanges();

            }

        }

    }

}

第二步:添加控制器,一个控制器控制一个视图

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MvcApplication2.Models;



namespace MvcApplication2.Controllers

{

    public class HomeController : Controller

    {

        //

        // GET: /Home/



        public ActionResult Index()

        {

            ViewBag.Data = new InfoBf().Select();

            return View();

        }

        //显示添加界面

        public ActionResult Add()

        {

            return View();

        }

        public ActionResult Insert(string sno,string sname,string ssex,DateTime sbirthdy,string @class)

        {

            new InfoBf().Insert(sno,sname,ssex,sbirthdy,@class);

            return RedirectToAction("Index");

        }

        //显示修改界面

        public ActionResult Exit(string id)

        {

            student data=new InfoBf().Select(id);

            ViewBag.Data = data;

            return View();

        }

        //更新至数据库

        public ActionResult Update(string sno,string sname,string ssex,DateTime sbirthdy,string @class)

        {

            new InfoBf().Update(sno,sname,ssex,sbirthdy,@class);

            return RedirectToAction("Index"); 

        }

        //删除结束直接更新至数据库

        public ActionResult Delete(string id)

        {

            new InfoBf().Delete(id);

            return RedirectToAction("Index");

        }

    }

}

第三部:在每一个控制器下添加视图。

这是Index视图

@using MvcApplication2.Models;

@using MvcApplication2.Controllers;

@{

    Layout = null;

}



<!DOCTYPE html>



<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>Index</title>

</head>

    <style type="text/css">

        .tbhead 

        {

        background-color:navy;

        text-align:center;

        font-weight:bold;

        }

        .tbrow

         {

            background-color:#FFFFCC;

            text-align:center;

        }



    </style>

<body>

    <div>

        <table width="100%" cellpadding="5" cellspacing="1" bgcolor="blue" border ="0">

            <tr class="tbhead">

                  <td>学号</td>

                  <td>姓名</td>

                  <td>性别</td>

                  <td>年龄</td>

                  <td>班级</td>

                  <td>操作</td>

            </tr>

            @{

                List<student> list=ViewBag.Data as List<student>;

                foreach(student data in list)

                {

             <tr class="tbrow"> 

                  <td>@data.sno</td> 

                  <td>@data.sname</td>  

                  <td><img src="@("images/girl.png")"/></td>  

                  <td>@data.sbirthdy</td>  

                  <td>@(data.@class)</td> 

                  <td>

                      <a href="/Home/Exit/@data.sno">修改</a>

                      <a onclick="return confirm('确认要删除 @data.sname 吗?')" href="/Home/Delete/@data.sno">删除</a>

                  </td>   

             </tr>  

              }   

            }

        </table>

        <a href="/Home/Add">添加人员</a>

    </div>

</body>

</html>

这是添加视图

@using  MvcApplication2.Models;

@using  MvcApplication2.Controllers;@{

    Layout = null;

}



<!DOCTYPE html>



<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>Add</title>

</head>

<body>

    <div>

        <form id="f1" name="f1" action="/Home/Insert">

        <h1> 添加人员</h1>

        <div>

           

        @*学号: <input name="sno" id="sno" type="text"/><br/>

        姓名:<input name="sname" id="sname" type="text" /><br/>

        性别:<input name="ssex" id="ssex" type="text"  /><br/>

        年龄:<input name="sbirthdy" id="sbirthdy" type="text"  /><br/>

        班级:<input name="class" id="class" type="text"  /><br/>*@



        学号: <input name="sno" id="sno" type="text"/><br/>

        姓名:<input name="sname" id="sname" type="text" /><br/>

        性别:<input name="ssex" id="ssex" type="radio"value="" checked="checked"/ >男<input name="ssex" id="ssex1" type="radio" value ="" />女<br/>

        年龄:<input name="sbirthdy" id="sbirthdy" type="text"/><br/>

        班级:<input name="class" id="class" type="text"  /><br/>



            <input id="Submit1" type="submit" value="更新" />

   

       </div>

        </form>

    </div>

</body>

</html>


这是修改视图

@using  MvcApplication2.Models;

@using  MvcApplication2.Controllers;

@{

    Layout = null;

}



<!DOCTYPE html>



<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>Exit</title>

</head>

<body>

    <div>

        <form id="f1" name="f1" action="/Home/Update">

        <h1> 修改人员</h1>

        <div>

            @{student data= ViewBag.Data as student;

              if (data!=null)

              {

                  <text>

        学号: <input name="sno" id="sno" type="text"value="@data.sno" /><br/>

        姓名:<input name="sname" id="sname" type="text"value="@data.sname" /><br/>

        性别:<input name="ssex" id="ssex" type="text" value="@data.ssex" /><br/>

        年龄:<input name="sbirthdy" id="sbirthdy" type="text" value="@data.sbirthdy" /><br/>

        班级:<input name="class" id="class" type="text" value="@(data.@class)" /><br/>

            <input id="Submit1" type="submit" value="更新" />

            </text>

            }

            }

       </div>

        </form>

    </div>

</body>

</html>

 

你可能感兴趣的:(html)