简体   繁体   中英

How do I display a record of database in asp.net mvc?

I m working with asp.net with MVC code-first approaches.

In my database, 4 records and I want to display record but a record not display?

table name: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>

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?? so that is issue bind the data from the database.??still try?

Try using @Html.DisplayFor instead of displaying the data directly.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM