本文介绍了如何在不使用实体框架的情况下从mvc4中的数据库获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是代码
This is code
[HttpPost] public ActionResult GetAllCompanies() { //public IEnumerable<CompanyDetails> GetCars() { using (var con = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString)) { var cmd = new SqlCommand("GetAllCompanies", con); cmd.CommandType = CommandType.StoredProcedure; try { if (con.State != ConnectionState.Open) con.Open(); cmd.ExecuteNonQuery(); return View(); } finally { if (con.State != ConnectionState.Closed) con.Close(); } } }我的.cshtml页面中的 以下我没有进入foreach所有重新获得的数据...
in my .cshtml page below am not getting in foreach where all the retrived data will come ...
@using(Html.BeginForm()) { @foreach(var item in) { @ Html.DisplayFor(modelItem => item。) @ Html.DisplayFor(modelItem => item。) @ Html.DisplayFor(modelItem => ;项目。) @ Html.DisplayFor(modelItem => item。) @ Html.DisplayFor(modelItem => item。) @ Html.ActionLink(编辑,编辑)| @ Html.ActionLink(详细信息,详细信息)| @ Html.ActionLink (删除,删除) } } @using (Html.BeginForm()) { @foreach (var item in ) { @Html.DisplayFor(modelItem => item.) @Html.DisplayFor(modelItem => item.) @Html.DisplayFor(modelItem => item.) @Html.DisplayFor(modelItem => item.) @Html.DisplayFor(modelItem => item.) @Html.ActionLink("Edit", "Edit") | @Html.ActionLink("Details", "Details") | @Html.ActionLink("Delete", "Delete") } }推荐答案
好的。你有三种方法: 1)使用其他ORM,而不是EF。那里有很多。此外:为什么不EF? - 存储过程不是问题,请参阅此 [ ^ ]。 2)创建结果集的模型类,基于该类的通用列表,并使用旧的ADO.NET工具将所有数据提取到该列表中。缺点:您必须立即获取所有内容,这可能会根据您的数据库变大。 3)在DataReader之上创建一个IEnumerable(参见这个 [ ^ ]),并将其作为模型传递给视图。 OK. You have three approaches: 1) Use an other ORM, not EF. There are plenty of them out there. Besides: why not EF? - stored procedure is not a problem, see this[^]. 2) Create a model class for your result set, a generic list based on that class, and fetch all data into that list with legacy ADO.NET tools. Drawback: you will have to fetch all at once, which can grow large depending on your database. 3) Create an IEnumerable on top of DataReader (see this[^]), and pass it to the View as model.
更多推荐
如何在不使用实体框架的情况下从mvc4中的数据库获取数据
发布评论