博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC test
阅读量:5225 次
发布时间:2019-06-14

本文共 5280 字,大约阅读时间需要 17 分钟。

1,index

@{    ViewBag.Title = "Index";}    
Index
) {
@foreach (test.Models.tb_name item in ViewData["DataList"] as List
}
id 姓名 状态 编辑
@item.Id @item.uName @item.uMark @Html.ActionLink("删除", "Del", new { id = item.Id }) @Html.ActionLink("修改", "Update", new { id = item.Id }) @Html.ActionLink("添加", "Add")

2,添加视图

@model test.Models.tb_name@{    ViewBag.Title = "Add";}    
Add

Create

@using (Html.BeginForm()) { @Html.ValidationSummary(true)
Student
@Html.LabelFor(model => model.uName)
@Html.EditorFor(model => model.uName) @Html.ValidationMessageFor(model => model.uName)
@Html.LabelFor(model => model.uMark)
@Html.EditorFor(model => model.uMark) @Html.ValidationMessageFor(model => model.uMark)

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

3,修改视图

@model test.Models.tb_name@{    ViewBag.Title = "Update";}    
Modify @using (Html.BeginForm("Update", "Index", FormMethod.Post)) {
修改:@Html.HiddenFor(a => a.Id)
课程名称 @Html.DropDownListFor(a => a.Id, ViewBag.classList as IEnumerable
)
学生姓名 @Html.TextBoxFor(a => a.uName)
@Html.ActionLink("返回", "Index", "Index")
}

4,控制器

public class IndexController : Controller    {        testEntities db = new testEntities();        //        // GET: /Index/        public ActionResult Index()        {            testEntities db = new testEntities();            List
list = (from d in db.tb_name select d).ToList(); ViewData["DataList"] = list; return View(); } /// 根据学生ID删除学生 /// ///
学生ID ///
[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
(modelDel).State = System.Data.EntityState.Deleted; db.SaveChanges(); return RedirectToAction("Index", "Index"); } ///
/// 根据学生编号修改学生 /// ///
///
[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
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] ///
/// 保存要修改的数据 /// ///
要修改的学生ID ///
public ActionResult Update(tb_name ts) { DbEntityEntry
entry = db.Entry
(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"); } ///
/// 添加 /// ///
[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); } }

  

转载于:https://www.cnblogs.com/Aamir-Ye/p/4598952.html

你可能感兴趣的文章
name phone email正则表达式
查看>>
721. Accounts Merge
查看>>
一个体验好的Windows 任务栏缩略图开发心得
查看>>
电商购物车产品在做什么?
查看>>
python数据类型之字典类型
查看>>
Python之Split函数
查看>>
Linux下使用pip安装keras
查看>>
OpenCv-Python 图像处理基本操作
查看>>
博物院与国宝
查看>>
vmware tools 的安装(Read-only file system 的解决)
查看>>
数列求和总结
查看>>
「Unity」委托 将方法作为参数传递
查看>>
Unity学习疑问记录之隐藏与显示物体
查看>>
设计模式-学习
查看>>
button标签点击实现数量加减
查看>>
重置GNOME-TERMINAL
查看>>
quartz 实现调度任务 SchedulerManager
查看>>
new jordans 9 Nets
查看>>
redis哨兵集群、docker入门
查看>>
[翻译][架构设计]The Clean Architecture
查看>>