Web MVC开发程序代码的简写方法,强类型数据和Html helper使用方法。车老师代码

控制器代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MvcApplication1.Models;



using System.IO;

namespace MvcApplication1.Controllers

{

    public class HomeController : Controller

    {

        

        public ActionResult Index()

        {

            List<Info> list = new InfoBF().Select();   

            //ViewBag.Data = new InfoBF().Select();    

            return View(list);  //强类型数据写法

        }

        public ActionResult Add()

        {

            Info data = new Info();

            List<Nation> list = new NationBF().Select();

            ViewBag.Data= new SelectList(list,"Code","Name"); 

            return View(data);   //强类型数据写法

        }

        public ActionResult Insert(Info data)   //强类型数据写法

        {

            new InfoBF().Insert(data);

            return RedirectToAction("Index");

        }

        //public ActionResult Insert(string code, string name, bool sex, string nation, DateTime birthday)

        //{

        //    new InfoBF().Insert(code, name, sex, nation, birthday);

        //    return RedirectToAction("Index");

        //}

        //显示出修改的界面来

        public ActionResult Edit(string id)

        {

            Info data = new InfoBF().Select(id);

            List<Nation> list = new NationBF().Select();

            SelectList options = new SelectList(list, "Code", "Name", data.Nation);

            ViewBag.Nations = options;

            return View(data);  //强类型数据写法

        }

        //更新回数据库

        public ActionResult Update(string code,string name,bool sex,string nation,DateTime birthday)

        {

            new InfoBF().Update(code, name, sex, nation, birthday);

            return RedirectToAction("Index");

        }

        public ActionResult Delete(string id)

        {

            new InfoBF().Delete(id);

            return RedirectToAction("Index");

        }



        //using 的用法

        public void Test()

        {

            //using (SqlConnection conn = new SqlConnection("..."))

            //{

            //    using (SqlCommand cmd = conn.CreateCommand)

            //    {

            //        cmd.CommandText = "";

            //        using (SqlDataReader dr = cmd.ExecuteReader())

            //        {



            //        }

            //    }

            //}



            //using (StreamWriter writer = new StreamWriter())

            //{

            //    writer.Write("adsf");

                

            //}



            //StreamWriter writer = null;

            //try

            //{

            //    writer = new StreamWriter();



            //    writer.Write("asdfsa");

            //    return;

            //}



            //catch

            //{

            //}

            //finally

            //{

            //    writer.Close();

            //}

        }

    }

}

MOdel里的增删改查方法

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



namespace MvcApplication1.Models

{

    public class InfoBF

    {

        private MyDBDataContext _Context = new MyDBDataContext();

        public List<Info> Select()

        {

            return _Context.Info.ToList();

        }

        public Info Select(string code)

        {

            var query = _Context.Info.Where(p=>p.Code == code);

            if (query.Count() > 0)

            {

                return query.First();

            }

            return null;

        }

        public void Insert(string code, string name, bool sex, string nation, DateTime birthday)

        {

            Info data = new Info

            {

                Code = code,

                Name = name,

                Sex = sex,

                Nation = nation,

                Birthday = birthday

            };

            _Context.Info.InsertOnSubmit(data);

            _Context.SubmitChanges();

        }

        //添加数据,强类型数据传值方法。非常简单

        public void Insert(Info data)

        {

            _Context.Info.InsertOnSubmit(data);

            _Context.SubmitChanges();

        }

        public void Delete(string code)

        {

            var query = _Context.Info.Where(p=>p.Code == code);

            if (query.Count() > 0)

            {

                Info data = query.First();

                _Context.Info.DeleteOnSubmit(data);

                _Context.SubmitChanges();

            }

        }

        public void Update(string code, string name, bool sex, string nation, DateTime birthday)

        {

            var query = _Context.Info.Where(p => p.Code == code);

            if (query.Count() > 0)

            {

                Info data = query.First();

                data.Name = name;

                data.Sex = sex;

                data.Nation = nation;

                data.Birthday = birthday;

                _Context.SubmitChanges();

            }

        }

    }

}


视图里的Html代码

@using MvcApplication1.Models;

@model List<Info>

@{

    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;

        }

        .tbrow {

            text-align:center;

            background-color:#FFFFcc;

        }

    </style>

</head>

<body>

    <div>

        <table id="tbinfo" width="100%" cellpadding="4" cellspacing="1" bgcolor="black" border="0">

            <tr class="tbhead">

                <td>代号</td>

                <td>姓名</td>

                <td>性别</td>

                <td>民族</td>

                <td>生日</td>

                <td>操作</td>

            </tr>

  

          @{ 

              foreach( Info data in Model)

              {

            <tr class="tbrow">

                <td>@data.Code</td>

                <td>@data.Name</td>

                <td><img src="@(data.Sex.Value?"images/male.png":"images/female.png")" /></td>

                <td>@data.Nation</td>

                <td>@data.Birthday</td>

                <td>



                    @* Html帮助的用法,主要用于form表单和超链接*@

                    @Html.ActionLink("修改", "Edit", "Home", new {id=data.Code}, new { })

                    @Html.ActionLink("删除", "Delete", "Home", new { id = data.Code }, new { onclick="return confirm('确认要删除吗?')"})

                </td>

            </tr>

            }

          }

        </table>

        @Html.ActionLink("添加人员", "Add", "Home")

    </div>

</body>

</html>
@using MvcApplication1.Models;

@model Info

@{

    Layout = null;

}



<!DOCTYPE html>



<html>

<head>

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

    <title>Edit</title>

</head>

<body>

    <div>

        @* Html帮助form 表单的用法*@

        @using(Html.BeginForm("Update","Home", FormMethod.Post)){

        <h1>修改人员</h1>

        <div>

            @{

                //Info data = ViewBag.Data as Info;

                //if(data != null)

                if(Model != null)

                {

           @* 下面这段代码非常重要!!!!!!!!!!!*@

            <div>

            代号:@Html.TextBoxFor(p=>p.Code) @*@Html.TextBox("code",Model.Code)*@<br/>

            姓名:@Html.TextBoxFor(m=>m.Name)@*@Html.TextBox("name",Model.Name)*@<br/>

            性别:@Html.RadioButtonFor(sg=>sg.Sex,"true") @*@Html.RadioButton("sex","true",Model.Sex == true)*@男 @Html.RadioButtonFor(p=>p.Sex,"false") @*@Html.RadioButton("sex","false",Model.Sex == false)*@女<br/>

            民族:@Html.DropDownListFor(p=>p.Nation,ViewBag.Nations as SelectList) @*@Html.DropDownList("nation",ViewBag.Nations as SelectList)*@ <br/>

            生日:@Html.TextBoxFor(p=>p.Birthday) @*@Html.TextBox("birthday",Model.Birthday)*@ <br/>

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

            </div>

                }

            }

        </div>

        }

        

    </div>

</body>

</html>
@using MvcApplication1.Models;

@model Info

@{

    Layout = null;

}



<!DOCTYPE html>



<html>

<head>

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

    <title>Add</title>

    <style type="text/css">

        .test {

            border:0px;

            border-bottom:1px solid black;

        }

    </style>

</head>

<body>

    <div>



        @*下面这段代码非常重要!!!!!!!!!!!!!!!!!!!!!!!!!!*@

        @*@{Html.BeginForm("Insert", "Home", FormMethod.Post);}*@

        @using(Html.BeginForm("Insert","Home", FormMethod.Post)){

        <h1>添加人员</h1>

        <div>

          

            <div>

            代号:@Html.TextBoxFor(p=>p.Code) @*@Html.TextBox("code",  "",new { @class="test"})*@<br/>

            姓名:@Html.TextBox("name", "", new { @class="test"})<br/>

            性别:@Html.RadioButton("sex","true",true) 男 @Html.RadioButton("sex","false") 女<br/>

            民族:@Html.DropDownList("nation",ViewBag.Data as SelectList)<br/>

            生日:@Html.TextBox("birthday", "", new { @class="test"})<br/>

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

               

            </div>

           

        </div>

        }

        

        @*@{Html.EndForm();}*@

    </div>

</body>

</html>

 

你可能感兴趣的:(html)