How to display database records in asp.net mvc view

前端 未结 2 608
旧巷少年郎
旧巷少年郎 2020-12-07 23:12

Using ASP.NET MVC with C#, how do you pass some database records to a View and display them in table form?

I need to know how I can transfer/pass some rows of record

2条回答
  •  无人及你
    2020-12-07 23:52

    If you dont have to use an sql reader would it not be easier to have the Controller like this.

    Controller.cs

    private ConnectContext db = new ConnectContext();
    
    public ActionResult Index()
       {
         return View(db.Tv.ToList());
       }
    

    ConnectContext.cs

    public class ConnectContext : DbContext
    {
        public DbSet Student{ get; set; }
    }
    

    This way your connection string will be in your web.config and the View + Model will remain the same.

提交回复
热议问题