MVC test

1,index

@{

    ViewBag.Title = "Index";

}



<!DOCTYPE html>



<html>

<head>

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

    <title>Index</title>

    <style type="text/css">

        #tblist

        {

            border: 1px solid #0094ff;

            width: 600px;

            margin: 10px auto;

            border-collapse: collapse;

        }



            #tblist th, td

            {

                border: 1px solid #0094ff;

                padding: 10px;

            }

    </style>

</head>

<body>

    <table id="tblist">

        <tr>

            <th>id</th>

            <th>姓名</th>

            <th>状态</th>

            <th>编辑</th>

        </tr>

        <!--变量action方法 设置viewData的集合数据生成html-->

        @foreach (test.Models.tb_name item in ViewData["DataList"] as List<test.Models.tb_name>)

        {

            <tr>

                <td>@item.Id</td>

                <td>@item.uName</td>

                <td>@item.uMark</td>

                <td>

                @Html.ActionLink("删除", "Del", new { id = item.Id })

                @Html.ActionLink("修改", "Update", new { id = item.Id })

                @Html.ActionLink("添加", "Add")

            </tr>

        }

    </table>



</body>

</html>

2,添加视图

@model test.Models.tb_name

@{

    ViewBag.Title = "Add";

}

<!DOCTYPE html>



<html>

<head>

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

    <title>Add</title>

    <style type="text/css">

        #tblist

        {

            border: 1px solid #0094ff;

            width: 600px;

            margin: 10px auto;

            border-collapse: collapse;

        }



            #tblist th, td

            {

                border: 1px solid #0094ff;

                padding: 10px;

            }

    </style>

</head>

<body>

    <h2>Create</h2>





    @using (Html.BeginForm())

    {

        @Html.ValidationSummary(true)

        <fieldset>

            <legend>Student</legend>



            <div class="editor-label">

                @Html.LabelFor(model => model.uName)

            </div>

            <div class="editor-field">

                @Html.EditorFor(model => model.uName)

                @Html.ValidationMessageFor(model => model.uName)

            </div>

            <div class="editor-label">

                @Html.LabelFor(model => model.uMark)

            </div>

            <div class="editor-field">

                @Html.EditorFor(model => model.uMark)

                @Html.ValidationMessageFor(model => model.uMark)

            </div>

            <p>

                <input type="submit" value="Create" />

            </p>

        </fieldset>

    }

    <div>

        @Html.ActionLink("Back to List", "Index")

    </div>

</body>

</html>

3,修改视图

@model test.Models.tb_name

@{

    ViewBag.Title = "Update";

}



<!DOCTYPE html>



<html>

<head>

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

    <title>Modify</title>

    <style type="text/css">

        #tblist

        {

            border: 1px solid #0094ff;

            width: 600px;

            margin: 10px auto;

            border-collapse: collapse;

        }



            #tblist th, td

            {

                border: 1px solid #0094ff;

                padding: 10px;

            }

    </style>

</head>

<body>

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

    {

        <table id="tblist">

            <tr>

                <td colspan="2">修改:@Html.HiddenFor(a => a.Id)</td>

            </tr>

            <tr>

                <td>课程名称</td>

                <!--使用HtmlHepler,直接从model获取数据赋值给下拉框-->

                <td>@Html.DropDownListFor(a => a.Id, ViewBag.classList as IEnumerable<SelectListItem>)</td>

            </tr>

            <tr>

                <td>学生姓名</td>

                <!--使用HtmlHepler,直接从model获取数据赋值给文本框-->

                <td>@Html.TextBoxFor(a => a.uName)</td>

            </tr>

            <tr>

                <td colspan="2">

                    <input type="submit" value="确定修改">@Html.ActionLink("返回", "Index", "Index")</td>

            </tr>

        </table>

    }

</body>

</html>

4,控制器

public class IndexController : Controller

    {



        testEntities db = new testEntities();

        //

        // GET: /Index/



        public ActionResult Index()

        {

            testEntities db = new testEntities();

            List<Models.tb_name> list = (from d in db.tb_name select d).ToList();

            ViewData["DataList"] = list;

            return View();

        }



        /// 根据学生ID删除学生

        /// </summary>

        /// <param name="id">学生ID</param>

        /// <returns></returns>

        [HttpGet]

        public ActionResult Del(string id)

        {

            testEntities db = new testEntities();

            int ids = Convert.ToInt32(id);

            tb_name modelDel = new tb_name() { Id = ids };

            db.tb_name.Attach(modelDel);

            db.Entry<tb_name>(modelDel).State = System.Data.EntityState.Deleted;

            db.SaveChanges();

            return RedirectToAction("Index", "Index");

        }





        /// <summary>

        /// 根据学生编号修改学生

        /// </summary>

        /// <param name="id"></param>

        /// <returns></returns>

        [HttpGet]

        public ActionResult Update(string id)

        {

            int ids = Convert.ToInt32(id);

            tb_name ts = (from a in db.tb_name where a.Id == ids select a).FirstOrDefault();

            IEnumerable<SelectListItem> listItem = (from c in db.tb_name select c).ToList().Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.uName });



            ViewBag.classList = listItem;

            return View(ts);



        }





        [HttpPost]

        /// <summary>

        /// 保存要修改的数据

        /// </summary>

        /// <param name="id">要修改的学生ID</param>

        /// <returns></returns>

        public ActionResult Update(tb_name ts)

        {

            DbEntityEntry<tb_name> entry = db.Entry<tb_name>(ts);

            entry.State = System.Data.EntityState.Unchanged;

            entry.Property(a => a.uName).IsModified = true;

            entry.Property(a => a.Id).IsModified = true;

            db.SaveChanges();

            return RedirectToAction("Index", "Index");

        }





        public ActionResult Add()

        {

            return View("Add");

        }



        /// <summary>

        /// 添加

        /// </summary>

        /// <returns></returns>

        [HttpPost]

        public ActionResult Add(tb_name student)

        {

            try

            {

                if (ModelState.IsValid)

                {

                    EntityState statebefore = db.Entry(student).State;  //Detached 

                    db.tb_name.Add(student);

                    EntityState stateAdd = db.Entry(student).State; //Added

                    db.SaveChanges();

                    EntityState stateafter = db.Entry(student).State;//Unchanged

                    return RedirectToAction("Index");

                }

            }

            catch

            {

                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");





            }

            return View(student);

        }

    }

  

你可能感兴趣的:(test)