C# 是否可以在ASP.NET中有一个与4个SQL表链接的创建页面,以使其更方便用户?
我正在使用C#、MVC和实体框架创建一个网站,该网站将显示数据并将数据输入车辆数据库。到目前为止,我已经为每个表控制器创建了一个页面。是否可以有一个创建页面显示并允许用户输入数据,而无需转到控制器的每个不同创建页面?这就是我要找的。但是,因为它只是我在SQLServerExpress中创建的一个视图,所以它是只读的,我无法对其进行写入 这就是我想要实现的,这样输入数据的方式更加用户友好 这就是当我从我的VehicleView控制器打开索引页时的样子C# 是否可以在ASP.NET中有一个与4个SQL表链接的创建页面,以使其更方便用户?,c#,sql-server,asp.net-mvc,entity-framework,C#,Sql Server,Asp.net Mvc,Entity Framework,我正在使用C#、MVC和实体框架创建一个网站,该网站将显示数据并将数据输入车辆数据库。到目前为止,我已经为每个表控制器创建了一个页面。是否可以有一个创建页面显示并允许用户输入数据,而无需转到控制器的每个不同创建页面?这就是我要找的。但是,因为它只是我在SQLServerExpress中创建的一个视图,所以它是只读的,我无法对其进行写入 这就是我想要实现的,这样输入数据的方式更加用户友好 这就是当我从我的VehicleView控制器打开索引页时的样子 namespace VehiclesFina
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);
}
}
}
这就是我创建视图的位置和方式,以便用户只看到他们正在查找的信息
这是我在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);
}
}
}
当然,这是可能的。创建包含所需所有数据字段的视图模型。填充视图模型,并将其发送到视图。允许编辑字段并在其上抛出保存按钮。表单将其全部发回控制器。在控制器操作中,您需要进行多次保存以更新每个表,但这并不是什么大问题。因此,由于我的视图已经有了一个控制器,我只需要找出一种方法来将更改保存到4个不同表的控制器操作中?您不需要为每个数据库表使用单独的控制器。例如,AccountController将处理有关用户及其帐户的所有信息,这些信息可能跨越多个表。在控制器操作中,您应该(如果您正确设置了表单等)了解您需要了解的所有信息。然后,根据更新的信息对模型进行适当的更改,并执行db保存。这就是实体框架的全部功能。