lundi 25 janvier 2021

Core MVC Entity Frame Repository Pattern

Help me please. I am new .net core mvc and here. I am developing a very simple blog site. But I'm getting an error. Post class

    
using System.Collections.Generic;

namespace DataAccess
{
     public class Post
    {
        public int PostId { get; set; }
        public string PostTitle { get; set; }
        public string PostWriter { get; set; }
        public string PostDate { get; set; }
        public string PostİmageUrl { get; set; }
        public string PostContent { get; set; }
        public List<Post> Posts { get; set; }


    }


}
using System.Collections.Generic;
using System.Linq;
using DataAccess.Abstract;
using Microsoft.EntityFrameworkCore;
using static DataAccess.Entity;

namespace DataAccess.Concrete.SqLite
{
    public class PostRepository : IPostRepository
    {
        private Context db = new Context ();
        public List<Post> GetAll()
        {
            return db.Posts.ToList();
        }

        public Post GetById(int id)
        {
            return db.Posts.Find(id);
        }
    }
}

Home Controller

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Blog.Models;
using DataAccess.Abstract;
using DataAccess;

namespace Blog.Controllers
{
    public class HomeController : Controller
    {
       
        private IPostRepository _postRepository;
        private ICommentRepository _commentRepository;
        private ICategoryRepository _categoryRepository;

        public HomeController(IPostRepository postRepository, ICommentRepository commentRepository, ICategoryRepository categoryRepository)
        {
            this._postRepository = postRepository;
            this._commentRepository = commentRepository;
            this._categoryRepository = categoryRepository;
        }
        public IActionResult Index()
        {
            var Post = new Post ()
            {
                Posts = _postRepository.GetAll()
            };
            return View(Post);
            // return View(new PostViewModel()
            // {
            //     Posts = _postRepository.GetAll()
            // });
            
        }

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

My View

@model Post

@{

  var P = Model.Posts;
}
      <!-- Post Content Column -->
      <div class="col-lg-8">

        <!-- Title -->
        @foreach (var item in P)
        {
                 <h1 class="mt-4">@item.PostTitle</h1>
   
        }
        </div>

This is my code. [My database][1] [My error][2] [1]: https://ift.tt/2Ya1AUu [2]: https://ift.tt/3oogGk8 how do you think i can solve the problem?

Aucun commentaire:

Enregistrer un commentaire