【发布时间】:2016-12-14 10:43:48
【问题描述】:
我收到类似上述标签的错误,该标签将位于
的位置返回视图(st.employees.Find(id));
只有上面的地方,任何人都可以帮助我!我的代码是
namespace StartApp.Controllers
{
public class EmployController : Controller
{
StartEntities st = new StartEntities();
//List
public ActionResult List()
{
return View(st.employees.ToList());
}
//Details
public ActionResult Details(int id = 0)
{
return View(st.employees.Find(id));
}
//Create
public ActionResult Create()
{
return View();
}
[HttpPost,ValidateAntiForgeryToken]
public ActionResult Create(employee e)
{
using(st)
{
st.employees.Add(e);
try
{
st.SaveChanges();
}
catch
{
System.Diagnostics.Debug.WriteLine("Here is an error");
}
}
return RedirectToAction("List");
}
//edit
public ActionResult Edit(int id = 0)
{
return View(st.employees.Find(id));
}
[HttpPost,ValidateAntiForgeryToken]
public ActionResult Edit(employee e)
{
st.Entry(e).State = EntityState.Modified;
st.SaveChanges();
return RedirectToAction("List");
}
//Delete
public ActionResult Delete(int id = 0)
{
return View(st.employees.Find(id));
}
[HttpPost,ActionName("Delete")]
public ActionResult Delete_conf(int id)
{
employee emp = st.employees.Find(id);
st.employees.Remove(emp);
st.SaveChanges();
return RedirectToAction("List");
}
}
}
谁能帮我纠正这个错误!
【问题讨论】:
-
看看
Employee实体。它的Key是什么类型的? -
它只是主键
-
你的类中键的数据类型是什么。 (查看 edmx 或您的代码文件中的字段属性)
标签: c# sql-server asp.net-mvc entity-framework linq-to-entities