如何在asp.net mvc中显示数据库记录? [英] How do I display a record of database in asp.net mvc?
问题描述
我正在使用MVC代码优先方法在asp.net上工作.
I m working with asp.net with MVC code-first approaches.
在我的数据库中,有4条记录,我想显示一条记录,但一条记录不显示?
In my database, 4 records and I want to display record but a record not display?
表名称:empls
如何获取数据库记录并显示在眉毛上
How can I get records of database and display on the brows
HomeController.cs
public class HomeController : Controller
{
EmpContext contextemp = new EmpContext();
// GET: Home
public ActionResult Index()
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
String sql = "SELECT * FROM empls";
var model = new List<Student>();
using (SqlConnection conn = new SqlConnection(cn)) //error //can not convert from system.data.sqlclient.sqlconnection to string
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
var student = new Student();
student.empname = (rdr["empname"].ToString());
student.empsalary = (rdr["empsalary"].ToString());
student.empage = (rdr["empage"].ToString()); // error cannot implicit type object to string
model.Add(student);
}
}
return View(model);
}
Index.cshtml
@model IEnumerable<DemoMvcInUpDecodefirstapproach.Models.Student>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.empname)
</th>
<th>
@Html.DisplayNameFor(model => model.empsalary)
</th>
<th>
@Html.DisplayNameFor(model => model.empage)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@item.empname
</td>
<td>
@item.empsalary
</td>
<td>
@item.empage
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
</table>
上下文类:
public class EmpContext : DbContext
{
public EmpContext():base("conn")
{
}
public virtual DbSet<Student> stud { get; set; }
}
学生班:
namespace DemoMvcInUpDecodefirstapproach.Models
{
public class Student
{
[Key]
public int empidid { get; set; }
public string empname { get; set; }
public string empsalary { get; set; }
public string empage { get; set; }
}
}
输出:
上述程序中我缺少什么?
What I m missing in the above program?
当我调试程序时,调试器从不检查foreach循环而不是index.cshtml?所以这是从数据库绑定数据的问题.仍然尝试吗?
when I m debugging my programme then my debugger never checks foreach loop instead of index.cshtml?? so that is issue bind the data from the database.??still try?
推荐答案
尝试使用@Html.DisplayFor
而不是直接显示数据.
Try using @Html.DisplayFor
instead of displaying the data directly.
示例:
@model IEnumerable<DemoMvcInUpDecodefirstapproach.Models.Student>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.empname)
</th>
<th>
@Html.DisplayNameFor(model => model.empsalary)
</th>
<th>
@Html.DisplayNameFor(model => model.empage)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.empname)
</td>
<td>
@Html.DisplayFor(modelItem => item.empsalary)
</td>
<td>
@Html.DisplayFor(modelItem => item.empage)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
</table>
希望它会为您工作.
这篇关于如何在asp.net mvc中显示数据库记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!