使下拉列表对用户而言是唯一的

时间:2012-04-06 17:43:24

标签: c# asp.net asp.net-mvc-3 asp.net-mvc-3-areas

大家好我有一个查询,说明每个游戏应该是唯一的帐户持有人工作正常,现在当我为评论写相同的查询我有一个问题有一个下拉选择所有的游戏然后可以编写评论的用户。我遇到的问题是下拉还显示其他用户添加的游戏我不想要,我希望添加的游戏对于添加了游戏的用户来说是唯一的。

这是我的评论控制器:

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);
            }
        }
    }

我用来使游戏独一无二并且评论独特的代码是:

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

我已将此代码添加到控制器的索引部分,并将以下代码添加到控制器的第一个创建部分:

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

如果还有其他要求,请告诉我吗?

所有表格的数据模型:

修改:

    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; }
    }
}

游戏:

 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; }
    }
}

控制台:

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; }
    }
}

1 个答案:

答案 0 :(得分:0)

在这一行中,您需要在.Where(...)上加tblGames来过滤掉您不想出现的游戏

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