hello I'm new in MVC and my project is CMS I read some articles about mvc and I understand the code first concept.
你好,我是MVC的新手,我的项目是CMS我读了一些关于mvc的文章,我理解代码的第一个概念。
My question is how to create controllers with existing Entity Framework? My colleagues here said that :
我的问题是如何使用现有的Entity Framework创建控制器?我的同事在这里说:
It should create an empty controller and create a new model for that controller but I don't know how.
它应该创建一个空控制器并为该控制器创建一个新模型,但我不知道如何。
Please help.
this are the codes generated when i added the controller with datacontext and model.
这是我使用datacontext和model添加控制器时生成的代码。
private SureSeatsDBEntities db = new SureSeatsDBEntities();
private SureSeatsDBEntities db = new SureSeatsDBEntities();
//
// GET: /Users/
public ActionResult Index()
{
return View(db.SystemUsers.ToList());
}
//
// GET: /Users/Details/5
public ActionResult Details(int id = 0)
{
SystemUser systemuser = db.SystemUsers.Find(id);
if (systemuser == null)
{
return HttpNotFound();
}
return View(systemuser);
}
//
// GET: /Users/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Users/Create
[HttpPost]
public ActionResult Create(SystemUser systemuser)
{
if (ModelState.IsValid)
{
db.SystemUsers.Add(systemuser);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(systemuser);
}
//
// GET: /Users/Edit/5
public ActionResult Edit(int id = 0)
{
SystemUser systemuser = db.SystemUsers.Find(id);
if (systemuser == null)
{
return HttpNotFound();
}
return View(systemuser);
}
//
// POST: /Users/Edit/5
[HttpPost]
public ActionResult Edit(SystemUser systemuser)
{
if (ModelState.IsValid)
{
db.Entry(systemuser).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(systemuser);
}
//
// GET: /Users/Delete/5
public ActionResult Delete(int id = 0)
{
SystemUser systemuser = db.SystemUsers.Find(id);
if (systemuser == null)
{
return HttpNotFound();
}
return View(systemuser);
}
//
// POST: /Users/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
SystemUser systemuser = db.SystemUsers.Find(id);
db.SystemUsers.Remove(systemuser);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
2 个解决方案
#1
0
Inside your ASP .NET MVC web application you will need to create a new controller. You do this by right clicking on the "Controllers" folder and navigating to "Add" -> "Controller". Specify your controller name and it will be created.
在ASP .NET MVC Web应用程序中,您需要创建一个新的控制器。您可以通过右键单击“控制器”文件夹并导航到“添加” - >“控制器”来执行此操作。指定您的控制器名称,它将被创建。
ASP .NET MVC controllers are not dependent on Entity Framework models unless you make them. You should create a view model for your controller, which is just a representation of what data is displayed on that page.
除非您创建它们,否则ASP .NET MVC控制器不依赖于Entity Framework模型。您应该为控制器创建一个视图模型,它只是表示该页面上显示的数据。
I suggest you do some searching to fill in the gaps. Things you may want to look up are "model view controller pattern".
我建议你做一些搜索填补空白。您可能想要查找的内容是“模型视图控制器模式”。
This series of tutorials is also a good starting point for learning all about ASP .NET MVC.
这一系列的教程也是学习ASP .NET MVC的一个很好的起点。
#2
0
You don't need to create the Models if you are using the Entity Framework as it will take care for you. And regarding the controller you do this by right clicking on the "Controllers" folder and navigating to
如果您使用实体框架,则无需创建模型,因为它会照顾您。关于控制器,您可以通过右键单击“控制器”文件夹并导航到该控制器来执行此操作
Add -> Controller
添加 - >控制器
State your controller name and it will be created.
说明您的控制器名称,它将被创建。
#1
0
Inside your ASP .NET MVC web application you will need to create a new controller. You do this by right clicking on the "Controllers" folder and navigating to "Add" -> "Controller". Specify your controller name and it will be created.
在ASP .NET MVC Web应用程序中,您需要创建一个新的控制器。您可以通过右键单击“控制器”文件夹并导航到“添加” - >“控制器”来执行此操作。指定您的控制器名称,它将被创建。
ASP .NET MVC controllers are not dependent on Entity Framework models unless you make them. You should create a view model for your controller, which is just a representation of what data is displayed on that page.
除非您创建它们,否则ASP .NET MVC控制器不依赖于Entity Framework模型。您应该为控制器创建一个视图模型,它只是表示该页面上显示的数据。
I suggest you do some searching to fill in the gaps. Things you may want to look up are "model view controller pattern".
我建议你做一些搜索填补空白。您可能想要查找的内容是“模型视图控制器模式”。
This series of tutorials is also a good starting point for learning all about ASP .NET MVC.
这一系列的教程也是学习ASP .NET MVC的一个很好的起点。
#2
0
You don't need to create the Models if you are using the Entity Framework as it will take care for you. And regarding the controller you do this by right clicking on the "Controllers" folder and navigating to
如果您使用实体框架,则无需创建模型,因为它会照顾您。关于控制器,您可以通过右键单击“控制器”文件夹并导航到该控制器来执行此操作
Add -> Controller
添加 - >控制器
State your controller name and it will be created.
说明您的控制器名称,它将被创建。