Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
911 views
in Technique[技术] by (71.8m points)

model view controller - 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://i.stack.imgur.com/IOc10.png [2]: https://i.stack.imgur.com/5ADC2.png how do you think i can solve the problem?

question from:https://stackoverflow.com/questions/65890506/core-mvc-entity-frame-repository-pattern

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...