嗨人们是 MVC3 和学习的新手,我想知道当用户添加游戏时是否可以允许用户在我的网站上检查游戏是否已经输入。我想要这个功能,所以我网站上的任何游戏玩家都无法对同一款游戏进行全面评论。原因是我有一个用户可以在其中谈论游戏的页面。所以这就是为什么我想要一种在添加新游戏时检查数据库的方法,如果游戏存在的话。
我的控制器如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using PagedList;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Test.Models;
namespace Test.Controllers
{
public class GameController : Controller
{
private gamezoneDBEntities db = new gamezoneDBEntities();
//
// GET: /Game/
public ViewResult Index(string Ordering, string WordFilter, string DisplaySearchResults, int? CounterForPage)
{
{
var Info = db.tblGames.Include(x => x.tblConsole);
}
var Games = from b in db.tblGames
.Where(U => U.UserName == User.Identity.Name)
select b;
switch (Ordering)
{
case "HeadlineName":
Games = Games.OrderBy(b => b.GameName);
break;
case "DatePosted":
Games = Games.OrderBy(b => b.ReleaseYear);
break;
case "DiscriptionDate":
Games = Games.OrderBy(b => b.ReleaseYear);
break;
default:
Games = Games.OrderByDescending(b => b.ReleaseYear);
break;
}
int pageSize = 3;
int pageNumber = (CounterForPage ?? 1);
var PageNumberResults = Games.ToPagedList(pageNumber, pageSize);
ViewBag.PageNumberResults = Games.Count();
if (PageNumberResults.Any())
{
return View(PageNumberResults);
}
return View("Error");
}
[HttpPost]
public ActionResult Create(tblGame tblgame,
HttpPostedFileBase image1,
HttpPostedFileBase image2)
{
try
{
if (ModelState.IsValid)
{
if (image1 != null)
{
string image = image1.FileName;
tblgame.Image = image;
var image1Path = Path.Combine(Server.MapPath("~/Content/UploadImages"), image);
image1.SaveAs(image1Path);
}
if (image2 != null)
{
string Image2 = image2.FileName;
tblgame.Image2 = Image2;
var image2Path = Path.Combine(Server.MapPath("~/Content/UploadImages"), Image2);
image2.SaveAs(image2Path);
}
db.tblGames.Add(tblgame);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ConsoleNameIDFK = new SelectList(db.tblConsoles, "ConsoleName", "ConsoleName", tblgame.ConsoleNameIDFK);
return View(tblgame);
}
catch
{
return View("Upload_Image_Failed");
}
}
//
// GET: /Game/Create
public ActionResult Create()
{
ViewBag.ConsoleNameIDFK = new SelectList(db.tblConsoles, "ConsoleName", "ConsoleName");
return View(new tblGame { UserName = @User.Identity.Name });
}
public ViewResult Details(int id)
{
tblGame tblgame = db.tblGames.Find(id);
return View(tblgame);
}
//
// GET: /Game/Edit/5
public ActionResult Edit(int id)
{
tblGame tblgame = db.tblGames.Single(i => i.GameID == id);
ViewBag.ConsoleNameIDFK = tblgame.ConsoleNameIDFK;
return View(tblgame);
}
[HttpPost]
public ActionResult Edit(tblGame tblgame, HttpPostedFileBase Image, int id,
HttpPostedFileBase image2)
{
if (ModelState.IsValid)
{
if (Image != null)
{
string image = Image.FileName;
tblgame.Image = image;
var image1Path = Path.Combine(Server.MapPath("~/Content/UploadImages"), image);
Image.SaveAs(image1Path);
}
if (image2 != null)
{
string Image2 = image2.FileName;
tblgame.Image2 = Image2;
var image2Path = Path.Combine(Server.MapPath("~/Content/UploadImages"), Image2);
image2.SaveAs(image2Path);
}
db.tblGames.Attach(tblgame);
db.Entry(tblgame).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Edit");
}
ViewBag.ConsoleNameIDFK = new SelectList(db.tblConsoles, "ConsoleName", "ConsoleName", tblgame.ConsoleNameIDFK);
return View(tblgame);
}
//
// GET: /Game/Delete/5
public ActionResult Delete(int id)
{
tblGame tblgame = db.tblGames.Find(id);
return View(tblgame);
}
//
// POST: /Game/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
try
{
tblGame tblgame = db.tblGames.Find(id);
db.tblGames.Remove(tblgame);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View("Error");
}
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
我一直很难尝试查看游戏是否存在,因为我有让用户独一无二的代码。我需要这个代码,否则我可以有一个语句来检查所有游戏的数据库,如果游戏存在这个代码,我会抛出一个错误,这对我来说有点困难,这就是我来这里的原因。
我已将以下内容添加到我的控制器中:
[HttpPost]
public ActionResult Create(tblGame tblgame, HttpPostedFileBase image1, HttpPostedFileBase image2)
{
try
{
if (ModelState.IsValid)
{
var mygame = db.tblGames.Where(x => x.GameName == tblgame.GameName).SingleOrDefault();
if (mygame != null)
{
if (image1 != null)
{
string image = image1.FileName;
tblgame.Image = image;
var image1Path = Path.Combine(Server.MapPath("~/Content/UploadImages"), image);
image1.SaveAs(image1Path);
}
if (image2 != null)
{
string Image2 = image2.FileName;
tblgame.Image2 = Image2;
var image2Path = Path.Combine(Server.MapPath("~/Content/UploadImages"), Image2);
image2.SaveAs(image2Path);
}
db.tblGames.Add(tblgame);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
//otherwise we add a generic error to the model state
ModelState.AddModelError("", "A game review already exists");
}
}
}
catch
{
return View("Upload_Image_Failed");
}
//if arrive here the model is returned back to the view with the errors added
return View(tblgame);
}