[英]Validate a unique input field from a foreign key
i have a table rubriqueintermediare
with two fields rubriqueId
and rubriqueintermediareId
which is a foreign key from the table Rubrique
. 我有一个带有两个字段
rubriqueId
和rubriqueintermediareId
的表rubriqueintermediare
,这是来自表Rubrique
的外键。
My table Rubriqueintermediare
have a listbox of the libelle
field from the rubrique
table. 我的表
Rubriqueintermediare
有一个来自rubrique
表的libelle
字段的列表框。
When I add a new rubriqueintermediare I mustn't add an existing one in the rubriqueintermediare list. 当我添加新的rubriqueintermediare时,我不能在rubriqueintermediare列表中添加现有的。
How could I do this validation when I have just rubriqueintermediareId
and rubriqueId
in the table rubriqueintermediare
which are auto incremented in my sql server. 当表
rubriqueintermediare
只有rubriqueintermediareId
和rubriqueId
,该rubriqueintermediare
在SQL Server中自动递增,该如何进行验证。 ther is my methode add in the controller of rubriqueintermediare: 这是我的方法添加到rubriqueintermediare的控制器中:
[HttpPost]
public JsonResult Save([DataSourceRequest] DataSourceRequest dsRequest, RubriqueIntermediareVM vm)
{
try
{
ViewData["CodeRubrique"] = new SelectList(RefDataManager.GetRefData<RubriqueVM>(), "RubriqueId", "CODELIBELLE");
if (ModelState.IsValid)
{
RubriqueIntermediareVM rubi = ServiceApplicatif.Save(vm);
IEnumerable<RubriqueVM> rubrique = RefDataManager.GetRefData<RubriqueVM>() as IEnumerable<RubriqueVM>;
}
DataCache dataCache = new DataCache(CurrentSecurityContext.TenantID);
dataCache.DropDataCache<RubriqueIntermediareVM>();
return Json(new[] { vm }.ToDataSourceResult(dsRequest, ModelState));
}
catch (Exception ex)
{
LoggerRubriqueIntermediare.Error(string.Format("Exception : {0}", ex.Message.ToString()));
throw new Exception("Erreur lors de l'enregistrement.");
}
}
You have to do this validation into your database 您必须对数据库进行此验证
In store procedure 入店手续
if exists( select Count(id)
from Rubriqueintermediare
where @name=rubricname) -- Assuming rubricname is column
begin
--return field as you want
END
Else
Begin
--Insert as you want
End
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.