简体   繁体   English

如何在asp.net mvc 中显示数据库记录?

[英]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.

相关问题 ASP.NET MVC:如何显示多行文本? - ASP.NET MVC: How do I display multiline text? 如何在ASP.NET MVC 2中显示数据表? - How do I display a datatable in asp.net mvc 2? 如何在ASP.NET MVC中将表单提交到数据库中 - How do I submit a form into a database in asp.net MVC 如何连接到ASP.NET MVC中的现有数据库? - How do I connect to an existing database in ASP.NET MVC? 如何在 ASP.NET MVC 中记录鼠标悬停的工具提示中显示来自数据库字段的附加信息 - How to display additional information from a database field in tool tip on Mouse Over of record in ASP.NET MVC C#-如何从数据库到ASP.NET MVC中的视图显示特定记录 - C# - How to Display a specific record from a database to a view in ASP.NET MVC 如何在asp.net mvc中使用下拉列表在数据库中插入记录? - How to Insert a record in database using dropdownlist In asp.net mvc? 使用asp.net mvc3,如何以不同的格式显示数据如何存储在数据库中? - Using asp.net mvc3, how do I display data in a different format to how it's stored in the database? 如何将用户字段设置为当前记录的用户ASP.NET MVC的数据库记录添加 - How should I add a database record with user field set to currently logged user ASP.NET MVC 如何在asp.net mvc视图中显示数据库记录 - How to display database records in asp.net mvc view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM