none
Object Doesnot contain the defination?GetPosts , Comments , Comment , Users , Posts , SaveChanges , SubComments , RRS feed

  • Question

  • using Borker92.Models;
    using System.Data.Entity;
    using System.Linq;
    using System.Web.Mvc;

    namespace Borker92.Controllers
    {
        public class GetPostController : Controller
        {
            private object dbContext;

            // GET: GetPost
            public ActionResult GetPosts()
            {
                IQueryable<PostsVM> Posts = DbContext.GetPosts
                                                 .Select(p => new PostsVM
                                                 {
                                                     PostID = p.PostID,
                                                     Message = p.Message,
                                                     PostedDate = p.PostedDate.Value
                                                 }).AsQueryable();

                return View(Posts);
            }



            public PartialViewResult GetComments(int postId)
            {
                IQueryable<CommentsVM> comments = dbContext.Comments.Where(c => c.Post.PostID == postId)
                                         .Select(c => new CommentsVM
                                         {
                                             ComID = c.ComID,
                                             CommentedDate = c.CommentedDate.Value,
                                             CommentMsg = c.CommentMsg,
                                             Users = new UserVM
                                             {
                                                 UserID = c.User.UserID,
                                                 Username = c.User.Username,
                                                 imageProfile = c.User.imageProfile
                                             }
                                         }).AsQueryable();

                return PartialView("~/Views/Shared/MyComments.cshtml", comments);
            }


            [HttpPost]
            public ActionResult AddComment(CommentsVM comment, int postId)
            {
                //bool result = false;  
                Comment commentEntity = null;
                int userId = (int)Session["UserID"];

                var user = dbContext.Users.FirstOrDefault(u => u.UserID == userId);
                var post = dbContext.Posts.FirstOrDefault(p => p.PostID == postId);

                if (comment != null)
                {

                    commentEntity = new EDMX.Comment
                    {
                        CommentMsg = comment.CommentMsg,
                        CommentedDate = comment.CommentedDate,
                    };


                    if (user != null && post != null)
                    {
                        post.Comments.Add(commentEntity);
                        user.Comments.Add(commentEntity);

                        dbContext.SaveChanges();
                        //result = true;  
                    }
                }

                return RedirectToAction("GetComments", "Comments", new { postId = postId });
            }
            [HttpGet]
            public PartialViewResult GetSubComments(int ComID)
            {
                IQueryable<SubCommentsVM> subComments = dbContext.SubComments.Where(sc => sc.Comment.ComID == ComID)
                                           .Select(sc => new SubCommentsVM
                                           {
                                               SubComID = sc.SubComID,
                                               CommentMsg = sc.CommentMsg,
                                               CommentedDate = sc.CommentedDate.Value,
                                               User = new UserVM
                                               {
                                                   UserID = sc.User.UserID,
                                                   Username = sc.User.Username,
                                                   imageProfile = sc.User.imageProfile
                                               }
                                           }).AsQueryable();

                return PartialView("~/Views/Shared/MySubComments.cshtml", subComments);

            }
        }
    }
    Friday, June 1, 2018 10:51 AM

All replies

  • You are referencing dbContext.SubComments but dbContext at the top of your file is defined to be object. Object doesn't have any of these members, hence the error. Your dbContext variable needs to be defined as whatever (DbContext-derived) type you have defined. This looks like EF code so it'll be the EF DbContext you created and then added your various DbSet<…> properties to.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, June 1, 2018 1:57 PM
  • I am not geeting the point at all i am new to this

    Saturday, June 2, 2018 9:29 AM
  • Your variable dbContext is specified as having type Object. That means it is only going to have access to the members defined on Object. The type of a variable determines what data it holds and therefore what you can do with it. In your code you defined userId as an int. That means userId stores an integral value and therefore the only thing you can do with it is what makes sense for an integral value.

    To resolve your issue search in your code for a type that derives from DbContext. That will be the name of your DB context type. When you look at that type definition you should see the various members you are trying to use.

    //Just an example
    public class MyDbContext : DbContext
    {
       public DbSet<Comment> Comments { get;set; }
       public DbSet<Post> Posts { get; set; }
    }

    Replace the 'object' in your declaration of dbContext inside the controller with whatever type you found the definition of and then see if your compiler errors go away.

    //Just an example
    public class GetPostController : Controller
    {
       //private object dbContext;
       private MyDbContext dbContext;
    }
    

    Note that once you've fixed this issue your code should compile but you'll then crash at runtime. You also need to create an instance of the context and assign it to the dbContext variable. This depends upon how you're building your app. In most cases you'll use a container to pass the context to the constructor of the controller. But for this simple example just try newing up the object directly.

    //Just an example
    public class GetPostController : Controller
    {
        // Most implementations of DbContext require the
        // connection string to the database
        // As an example here we assume it is in some variable
        private MyDbContext dbContext = new MyDbContext(myConnectionString);
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, June 2, 2018 3:17 PM
  • Hi Muhammad Wahaj Zafar,

    Thank you for posting here.

    Since your question is more related to MVC, you could post a new thread in MVC forum for suitable support.

    https://forums.asp.net/1146.aspx/1?MVC

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 4, 2018 6:17 AM