[英]How do I display a record of database in asp.net mvc?
I m working with asp.net with MVC code-first approaches.我正在使用 MVC 代码优先方法使用 asp.net。
In my database, 4 records and I want to display record but a record not display?在我的数据库中,有 4 条记录,我想显示记录但没有显示一条记录?
table name:empls表名:empls
How can I get records of database and display on the brows如何获取数据库的记录并显示在眉毛上
HomeController.cs家庭控制器.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索引.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>
Context class:上下文类:
public class EmpContext : DbContext
{
public EmpContext():base("conn")
{
}
public virtual DbSet<Student> stud { get; set; }
}
Student class:学生班:
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; }
}
}
Output:输出:
What I m missing in the above program?我在上面的程序中缺少什么?
when I m debugging my programme then my debugger never checks foreach loop instead of index.cshtml??当我调试我的程序时,我的调试器从不检查 foreach 循环而不是 index.cshtml?? so that is issue bind the data from the database.??still try?所以这是从数据库绑定数据的问题。?还是试试?
Try using @Html.DisplayFor
instead of displaying the data directly.尝试使用@Html.DisplayFor
而不是直接显示数据。
Example :例子 :
@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>
Hope it'll work for you.希望它对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.