简体   繁体   English

ASP.NET如何从数据库获取数据以在网页上查看

[英]ASP.NET How to get data from database to view on webpage

Hello I have a database setup and running properly inside a .NET application. 您好,我有一个数据库设置程序,可以在.NET应用程序中正常运行。

However I am having trouble populating the views with content from the database. 但是,我无法用数据库中的内容填充视图。 Already I have installed to the Entity Framework and the Framework tools. 我已经安装了实体框架和框架工具。

Any help would be wonderful. 任何帮助都会很棒。

Here is my DB context 这是我的数据库上下文

using Microsoft.EntityFrameworkCore;

namespace _3241_farmDb.Entities
{
    public class FarmDbContext : DbContext
    {

        public FarmDbContext(DbContextOptions options) : base(options)
        {

        }
        public DbSet<Farmer> Farmers { get; set; }
        public DbSet<Farm> Farms {get; set;}
        public DbSet<Child> Children {get; set;}
        public DbSet<Attends> Attend {get; set;}
        public DbSet<Livestock> Livestocks {get; set;}
        public DbSet<Farm_Houses> Farm_Houses {get; set;}
        public DbSet<Crops> Crops {get; set;}

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Attends>()
                .HasKey(c => new { c.FarmerSS, c.HotelID });

            modelBuilder.Entity<Child>()
                .HasKey(c => new { c.FarmerSS, c.Fname, c.Lname });
        }

    }
}

Here is my HomeController 这是我的HomeController

using System.Linq;
using Microsoft.AspNetCore.Mvc;
using _3241_farmDb.Entities;

namespace _3241_farmDb.Controllers
{
    public class HomeController : Controller
    {
        private FarmDbContext _context;

        public HomeController(FarmDbContext context)
        {
            _context = context;
        }
        public IActionResult Index()
        {
            return View(_context.Farmers.AsQueryable()); //CHANGED THIS
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Error()
        {
            return View();
        }
    }
}

HomePageViewModel HomePageViewModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using _3241_farmDb.Entities;

namespace _3241_farmDb.ViewModel
{
    public class HomePageViewModel
    {
        public string CurrentMessage { get; set; }
        public IQueryable<Attends> Attend { get; set; }
        public IQueryable<Farmer> Farmers { get; set; }
        public IQueryable<Child> Children { get; set; }
        public IQueryable<Livestock> Livestock { get; set; }
        public IQueryable<Farm_Houses> Farm_Houses { get; set; }
        public IQueryable<Crops> Crops { get; set; }
    }
}

And Finally my index.cshtml View 最后是我的index.cshtml视图

@model IQueryable<_3241_farmDb.ViewModel.HomePageViewModel>

@{
    ViewBag.Title = "Home";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Welcome to the 3241 Farm Database</h1>
<table>
    @foreach (var farmers in Model)
    {
        <tr>
            <td>
                <a asp-action="Details" asp-route-id="">@farmers.SS#</a>
            </td>
            <td>@farmers.Fname</td>
        </tr>
    }
</table>
<a asp-action="Create">Create</a>

You will need to pass entity to model. 您将需要通过实体进行建模。

Model 模型

public class HomePageViewModel
{
    public IList<Farmer> Farmers { get; set; }
    public HomePageViewModel()
    {
        Farmers = new List<Farmer>();
    }
}

Controller 调节器

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new HomePageViewModel
        {
            Farmers = _context.Farmers.ToList()
        };
        return View(model);
    }
}

View 视图

@model _3241_farmDb.ViewModel.HomePageViewModel
<table>
    @foreach (var farmers in Model.Farmers)
    {
        <tr>
            <td>
                <a asp-action="Details" asp-route-id="">@farmers.SS#</a>
            </td>
            <td>@farmers.Fname</td>
        </tr>
    }
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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