0

Hello I have a database setup and running properly inside a .NET application.

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

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

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

@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>
4
  • 1
    Your controller is sending a List<Farmer> to the view, but the view expects an IQueryable<_3241_farmDb.ViewModel.HomePageViewModel>. Inside your controller's Index method, you need to retrieve the data and create an IQueryable<_3241_farmDb.ViewModel.HomePageViewModel>, sending that to the View. BTW, based on the view, you only need the Farmer data in your HomePageViewModel. Alternatively, you could just change the View to expect an IQueryable<Farmer> Commented Dec 1, 2016 at 17:45
  • Thanks for the advice. I am brand new to ASP.NET. An edit has been made to my homeController. Would this work? Commented Dec 1, 2016 at 17:49
  • 1
    No, you're still sending Farmers to the View rather than HomePageViewModel(s). See Win's answer below. Commented Dec 1, 2016 at 17:51
  • Thanks Matt I really appreciate it Commented Dec 1, 2016 at 17:52

1 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

3 Comments

I appreciate the advice, I am going to try and run this. Down the road we need to be able to execute SQL scripts involving the Farmer object. Wouldn't I need an IQueryable to do this?
I personally do not like returning IQueryable to view. I normally filter data at Service Layer. Since you do not have service layer, you want to filter inside action method. View should not be the responsibility of filtering the data.
Great advice! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.