是否可以在ASP.NET中使用一个与4个SQL表链接的创建页面,使其更加用户友好?

时间:2018-04-19 19:43:15

标签: c# sql-server asp.net-mvc entity-framework

我正在使用C#,MVC和Entity Framework来创建一个网站,该网站将显示数据并将其输入到车辆数据库中。到目前为止,我为每个表控制器都有一个创建页面。是否可以有一个创建页面显示并允许用户输入数据而无需转到控制器的每个不同的创建页面?这就是我要找的。但是因为它只是我在SQL Server Express中创建的一个视图,它是一个只读,我无法写入它。

这就是我想要完成的,这样输入数据的方式更加用户友好

Create Page For My SQL View With Table Joins

这是我从我的VehicleView控制器

中调出索引页面时的样子

Index View

这是我创建视图的地方和方式,以便用户只能看到他们要查找的信息。

SQL Join View

这是我在VehicleViewController中的代码

namespace VehiclesFinal.Controllers
{
    public class VehiclesViewsController : Controller
    {
        private DataConnection db = new DataConnection();

        // GET: VehiclesViews
        public ActionResult Index()
        {
            return View(db.VehiclesViews.ToList());
        }

        // GET: VehiclesViews/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            VehiclesView vehiclesView = db.VehiclesViews.Find(id);
            if (vehiclesView == null)
            {
                return HttpNotFound();
            }
            return View(vehiclesView);
        }

        // GET: VehiclesViews/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: VehiclesViews/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "VehicleId,Year,Price,Cost,Name,Model,Type,EngineSize,Transmission,NumberOfDoors,Colour,SoldDate")] VehiclesView vehiclesView)
        {
            if (ModelState.IsValid)
            {
                db.VehiclesViews.Add(vehiclesView);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(vehiclesView);
        }

        // GET: VehiclesViews/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            VehiclesView vehiclesView = db.VehiclesViews.Find(id);
            if (vehiclesView == null)
            {
                return HttpNotFound();
            }
            return View(vehiclesView);
        }

        // POST: VehiclesViews/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "VehicleId,Year,Price,Cost,Name,Model,Type,EngineSize,Transmission,NumberOfDoors,Colour,SoldDate")] VehiclesView vehiclesView)
        {
            if (ModelState.IsValid)
            {
                db.Entry(vehiclesView).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(vehiclesView);
        }

        // GET: VehiclesViews/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            VehiclesView vehiclesView = db.VehiclesViews.Find(id);
            if (vehiclesView == null)
            {
                return HttpNotFound();
            }
            return View(vehiclesView);
        }

        // POST: VehiclesViews/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            VehiclesView vehiclesView = db.VehiclesViews.Find(id);
            db.VehiclesViews.Remove(vehiclesView);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

0 个答案:

没有答案