简单的CRUD应用程序,我需要控制器中的帮助

时间:2019-06-24 17:38:07

标签: asp.net-mvc model-view-controller controller crud dbcontext

我完成了我简单的CRUD MVC Web应用程序,它运行良好,但是老师说控制器是简单的代码,我需要对其进行修复。他提到了上下文,但我不理解他。

如果有人可以帮助我,我会上传代码的图片,我是个初学者。

using GasStation.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GasStation.Controllers

{
    public class HomeController : Controller

    {
        public ActionResult Index(
        {
            return View();
        }

        public ActionResult GetOrders()
        {
            using (MyDatabaseEntities dc = new MyDatabaseEntities())
            {
                var orders = dc.Orders.OrderBy(a => a.FirstName).ToList();

                return Json(new { data = orders },
JsonRequestBehavior.AllowGet);
            }
        }

        [HttpGet]

        public ActionResult Save(int id)
        {
            using (MyDatabaseEntities dc = new MyDatabaseEntities())
            {

    var v = dc.Orders.Where(a => a.OrderID == id).FirstOrDefault();

                return View(v);
            }
        }

        [HttpPost]

        public ActionResult Save(Order emp)
        {
            bool status = false;

            if (ModelState.IsValid)
            {
                using (MyDatabaseEntities dc = new MyDatabaseEntities())
                {
                    if (emp.OrderID > 0)
                    {
                        var v = dc.Orders.Where(a => a.OrderID == 
emp.OrderID).FirstOrDefault();

                        if (v != null)
                        {
                            v.FirstName = emp.FirstName;
                            v.LastName = emp.LastName;
                            v.FuelType = emp.FuelType;
                            v.Quantity = emp.Quantity;
                            v.City = emp.City;
                            v.Address = emp.Address;
                        }
                    }
                    else
                    {
                        dc.Orders.Add(emp);
                    }

                    dc.SaveChanges();

                    status = true;
                }
            }

            return new JsonResult { Data = new { status = status } };
        }

        [HttpGet]

        public ActionResult Delete (int id)
        {
            using (MyDatabaseEntities dc = new MyDatabaseEntities())
            {
                var v = dc.Orders.Where(a => a.OrderID == 
id).FirstOrDefault();

                if (v != null)
                {
                    return View(v);
                }
                else
                {
                    return HttpNotFound();
                }
            }
        }

        [HttpPost]

        [ActionName("Delete")]

        public ActionResult DeleteOrder(int id)
        {
            bool status = false;

            using (MyDatabaseEntities dc = new MyDatabaseEntities())
            {
                var v = dc.Orders.Where(a => a.OrderID == 
id).FirstOrDefault();

                if (v != null)
                {
                    dc.Orders.Remove(v);
                    dc.SaveChanges();
                    status = true;
                }
            }

            return new JsonResult { Data = new { status = status } };
        }
    }
}

0 个答案:

没有答案