【问题标题】:How do I display a record of database in asp.net mvc?如何在 asp.net mvc 中显示数据库记录?
【发布时间】:2020-03-30 07:56:06
【问题描述】:

我正在使用 MVC 代码优先方法的 asp.net。

在我的数据库中,有 4 条记录,我想显示记录但没有显示记录?

表名:empls

如何获取数据库记录并显示在浏览器上

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; }
    }
}

输出:

我在上面的程序中缺少什么?

当我调试我的程序时,我的调试器从不检查 foreach 循环而不是 index.cshtml??所以这是问题绑定数据库中的数据。??还在尝试吗?

【问题讨论】:

  • 您是否在此处获取任何值:contextemp.stud.ToList()?也可以尝试在不使用 DisplayFor 模板的情况下打印出值:@item.empname,@item.empsalary,@item.empage
  • @RahulSharma 我编辑了你的建议,但仍然没有解决问题检查我的 index.cshtml(对于每个循环)我编辑了我的 index.cshtml
  • 您是否从数据库中获取列表中的任何值?你有没有试过调试,看看你得到了什么:contextemp.stud.ToList()
  • @RahulSharma 先生感谢您的建议,我尝试调试 c# count=0 显示还在尝试吗??
  • 您的实体与它应该指向的数据库表的名称不同。尝试将注释[Table("empls")] 添加到学生的班级,这是为了让 Rahul 的建议返回一些东西。正如达斯顿所说,您正在创建两个连接。只需使用上下文contextemp,它还允许您运行原始 sql 查询。

标签: c# asp.net model-view-controller


【解决方案1】:

尝试使用@Html.DisplayFor,而不是直接显示数据。

例子:

@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>

希望它对你有用。

【讨论】:

    猜你喜欢
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    相关资源
    最近更新 更多