大家好,我有一个查询,指出每个游戏都应该是唯一的帐户持有人,可以正常工作,现在当我为评论编写相同的查询时,我遇到了一个问题,有一个下拉菜单选择所有用户的游戏,然后可以写评论。遇到的问题是下拉菜单还显示了我不想要的其他用户添加的游戏,我希望添加的游戏对添加它们的用户来说是唯一的。
这是我的审查控制器:
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);
我已将此代码添加到 i 控制器的 index 部分,并将以下代码添加到控制器的第一个 create 部分:
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; }
}
}