1.今日任务:

①车辆信息添加

②车辆信息删除

③车辆信息修改

④车辆信息显示与搜索

2.核心代码和效果图:

public class CarsController : Controller
    {
        carSystemEntities db = new carSystemEntities();
        // GET: Cars
        [HttpGet]
        public ActionResult CarsIndex()
        {
            var list = db.t_car.ToList();
            return View(list);
        }

        [HttpGet]
        public ActionResult Edit(int? id) 
        {
            ViewBag.carinfo = db.t_car.Find(id);
            return View();
        }
        [HttpPost]
        public ActionResult Edit(t_car car) 
        {
            if (ModelState.IsValid)
            {
                db.Entry(car).State = EntityState.Modified;
                int i = db.SaveChanges();
                if (i > 0)
                {
                    return RedirectToAction("CarsIndex");
                }
            }
            return View();
        }

        public ActionResult Delete(int id)
        {
            //find()只能用于有主键表的查询
            t_car c = db.t_car.Find(id);
            db.t_car.Remove(c);
            int i = db.SaveChanges();
            if (i > 0)
            {
                return RedirectToAction("CarsIndex");
            }
            else
            {
                return View();
            }
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(t_car car)
        {
            if (ModelState.IsValid)
            {
                db.t_car.Add(car);
                db.SaveChanges();

                return RedirectToAction("CarsIndex");
            }
            return View(car);
        }
    }

 

 

 

 

3.遇到的问题

①显示的列数据无法排序;

②确认修改时数据无法传到控制器;

4.解决的方案:

①调整了js和列数据;

②修改了控件name使其能正确映射;

 

 posted on 2020-08-14 10:18  死鱼眼的坂田银  阅读(119)  评论(0)    收藏  举报