我需要在控制器内的创建和编辑方法中保存到数据库之前检查重复项。然后我需要显示重复的错误消息而不是通用错误消息。
我对重复的定义是:userid + Code1ID + Code2ID + Code3ID + Code4ID。
问题:当上述值的组合已经存在时,如何检查重复项?
表名
Character:
CharacterID int
UserID int
Code1ID int
Code2ID int
Code3ID int
Code4ID int
Name Varchar(40)
我的主键由 UserID + Code1ID + Code2ID + Code3ID + Code4ID 组成。这保证没有重复输入到数据库中。但我返回的错误消息是一般错误消息。
我需要先检查重复项,然后仅根据重复项返回警告消息或错误消息。
这是控制器内部的编辑方法
//
// GET: /Character/Edit/5
public ActionResult Edit(int id)
{
Character character = db.Characters.Find(id);
PopulateUserIDDropDownList(character.UserID);
PopulateCode1IDDropDownList(character.Code1ID);
PopulateCode2IDDropDownList(character.Code2ID);
PopulateCode3IDDropDownList(character.Code3ID);
PopulateCode4IDDropDownList(character.Code4ID);
return View(character);
}
//
// POST: /Character/Edit/5
[HttpPost]
public ActionResult Edit(Character character)
{
try
{
if (ModelState.IsValid)
{
db.Entry(character).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch
{
ModelState.AddModelError("", "Generic Error. Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
PopulateUserIDDropDownList(character.UserID);
PopulateCode1IDDropDownList(character.Code1ID);
PopulateCode2IDDropDownList(character.Code2ID);
PopulateCode3IDDropDownList(character.Code3ID);
PopulateCode4IDDropDownList(character.Code4ID);
return View(character);
}
这是控制器内部的创建方法
//
// GET: /Character/Create
public ActionResult Create()
{
PopulateUserIDDropDownList();
PopulateCode1IDDropDownList();
PopulateCode2IDDropDownList();
PopulateCode3IDDropDownList();
PopulateCode4IDDropDownList();
return View();
}
//
// POST: /Character/Create
[HttpPost]
public ActionResult Create(Character character)
{
try
{
if (ModelState.IsValid)
{
db.Characters.Add(character);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch
{
ModelState.AddModelError("", "Generic Error.Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
PopulateUserIDDropDownList(character.UserID);
PopulateCode1IDDropDownList(character.Code1ID);
PopulateCode2IDDropDownList(character.Code2ID);
PopulateCode3IDDropDownList(character.Code3ID);
PopulateCode4IDDropDownList(character.Code4ID);
return View(character);
}
模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace myproject.Models
{
public class Character
{
[Key]
public int CharacterID { get; set; }
[Required(ErrorMessage = "User is required.")]
[Display(Name = "User")]
public Guid UserId { get; set; }
[Required(ErrorMessage = "Code1 is required.")]
[Display(Name = "Code1")]
public int Code1ID { get; set; }
[Required(ErrorMessage = "Code2 is required.")]
[Display(Name = "Code2")]
public int Code2ID { get; set; }
[Required(ErrorMessage = "Code3 is required.")]
[Display(Name = "Code3")]
public int Code3ID { get; set; }
[Required(ErrorMessage = "Code4 is required.")]
[Display(Name = "Code4")]
public int Code4ID { get; set; }
[Required(ErrorMessage = "level is required.")]
[Display(Name = "Level")]
public int LevelID { get; set; }
public virtual aspnet_Users Aspnet_User { get; set; }
public virtual Code1 Code1 { get; set; }
public virtual Code2 Code2 { get; set; }
public virtual Code3 Code3 { get; set; }
public virtual Code4 Code4 { get; set; }
}
}