0

Hi people I have a query which states that each game should be unique to the account holder that works fine, now when i wrote the same query for the reviews I have a problem there is a drop down which selects games of all the user which then a review can be written. The problem am having is the drop down is also showing the games added by other users which i do no want, I want the games added to be unique to the user only that has added them.

here is my review controller:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameTest.Models;

    namespace GameTest.Controllers
    { 
        public class ReviewController : Controller
        {
            private gamezoneDBEntities db = new gamezoneDBEntities();

            //
            // GET: /Review/

            public ViewResult Index()
            {
                var Info = db.tblReviews.Include(x => x.tblGame).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
                return View(Info); 
            }

            //
            // GET: /Review/Details/5

            public ViewResult Details(int id)
            {
                tblReview tblreview = db.tblReviews.Find(id);
                return View(tblreview);
            }

            //
            // GET: /Review/Create

            public ActionResult Create()
            {
                ViewBag.GameIDFK = new SelectList(db.tblGames, "GameID", "GameName");
                return View(new tblReview { UserName = @User.Identity.Name });
            } 

            //
            // POST: /Review/Create

            [HttpPost]
            public ActionResult Create(tblReview tblreview)
            {
                if (ModelState.IsValid)
                {
                    db.tblReviews.Add(tblreview);
                    db.SaveChanges();
                    return RedirectToAction("Index");  
                }

                ViewBag.GameIDFK = new SelectList(db.tblGames, "GameID", "GameName", tblreview.GameIDFK);
                return View(tblreview);
            }

            //
            // GET: /Review/Edit/5

            public ActionResult Edit(int id)
            {
                tblReview tblreview = db.tblReviews.Find(id);
                ViewBag.GameIDFK = new SelectList(db.tblGames, "GameID", "GameName", tblreview.GameIDFK);
                return View(tblreview);
            }

            //
            // POST: /Review/Edit/5

            [HttpPost]
            public ActionResult Edit(tblReview tblreview)
            {
                if (ModelState.IsValid)
                {
                    db.Entry(tblreview).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                ViewBag.GameIDFK = new SelectList(db.tblGames, "GameID", "GameName", tblreview.GameIDFK);
                return View(tblreview);
            }

            //
            // GET: /Review/Delete/5

            public ActionResult Delete(int id)
            {
                tblReview tblreview = db.tblReviews.Find(id);
                return View(tblreview);
            }

            //
            // POST: /Review/Delete/5

            [HttpPost, ActionName("Delete")]
            public ActionResult DeleteConfirmed(int id)
            {            
                tblReview tblreview = db.tblReviews.Find(id);
                db.tblReviews.Remove(tblreview);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            protected override void Dispose(bool disposing)
            {
                db.Dispose();
                base.Dispose(disposing);
            }
        }
    }

The code that i have used to make the games unique and the review unique is this:

var Info = db.tblReviews.Include(x => x.tblGame).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
            return View(Info); 

I have added this code to the index section of thei controller and the following code to the first create section of controller:

ViewBag.GameIDFK = new SelectList(db.tblGames, "GameID", "GameName");
            return View(new tblReview { UserName = @User.Identity.Name });
        } 

If there is any thing else your require please let me know?

Data Model For All Tables:

Review:

    namespace GameTest.Models
{
    using System;
    using System.Collections.Generic;

    public partial class tblReview
    {
        public int ReviewID { get; set; }
        public string Recomendation { get; set; }
        public string AvoidOrBuy { get; set; }
        public string Score { get; set; }
        public int GameIDFK { get; set; }
        public string UserName { get; set; }

        public virtual tblGame tblGame { get; set; }
    }
}

Game:

 namespace GameTest.Models
{
    using System;
    using System.Collections.Generic;

    public partial class tblGame
    {
        public tblGame()
        {
            this.tblReviews = new HashSet<tblReview>();
        }

        public int GameID { get; set; }
        public string GameName { get; set; }
        public string ReleaseYear { get; set; }
        public string Cost { get; set; }
        public string Description { get; set; }
        public string Downloads { get; set; }
        public string Image { get; set; }
        public string ConsoleNameIDFK { get; set; }
        public string UserName { get; set; }

        public virtual tblConsole tblConsole { get; set; }
        public virtual ICollection<tblReview> tblReviews { get; set; }
    }
}

Console:

namespace GameTest.Models
{
    using System;
    using System.Collections.Generic;

    public partial class tblConsole
    {
        public tblConsole()
        {
            this.tblGames = new HashSet<tblGame>();
        }

        public string ConsoleName { get; set; }

        public virtual ICollection<tblGame> tblGames { get; set; }
    }
}
user1137472
  • 345
  • 2
  • 5
  • 20
  • It seems you just need to populate the enumerable list of games referencing the current user, rather than the complete list. – Jeremy Holovacs Apr 06 '12 at 17:52
  • How do i do that? could you provide some help towards this? as i have provided a lot of my code and a attempt to solve it thank you for you kind answer – user1137472 Apr 06 '12 at 17:54
  • You need to examine your persistence layer. You are not building your SelectList correctly; you need to filter out the items that are out of scope for the current user. I really can't tell you any more detail without understanding your data model. – Jeremy Holovacs Apr 06 '12 at 17:58
  • I will post my datamodel, do u require the entire database or the table? – user1137472 Apr 06 '12 at 18:09
  • I have edited my question with my datamodel fro all tables – user1137472 Apr 06 '12 at 18:14

1 Answers1

0

in this line, you need to stick a .Where(...) on tblGames to filter out the games you don't want to appear

ViewBag.GameIDFK = new SelectList(db.tblGames, "GameID", "GameName");
        return View(new tblReview { UserName = @User.Identity.Name });
    } 
Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • I am getting the following error: The model item passed into the dictionary is of type GameTest.Models.tblReview', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[GameTest.Models.tblReview]'. – user1137472 Apr 06 '12 at 18:25
  • I want the games to be unique to user that posted them, so say you posted a game on my website and i posted a game i should not be able to write an review on your games – user1137472 Apr 06 '12 at 18:33
  • change `db.tblGames` in your model to be `IEnumerable` instead of `ICollection`, see if that makes a difference. – Jeremy Holovacs Apr 06 '12 at 21:25