我认为远程验证可以帮助您解决这个问题。您可以使用它来检查目录号是否存在于数据库中:
public class Line
{
[Remote("QueryCatalogNumberExists", "Home")]
public int CatalogNumber { get; set; }
public int TeamCode { get; set; }
}
然后在你的控制器中(我没有测试过这段代码,但它应该是类似的东西):
public JsonResult QueryCatalogNumberExists(int catalogNumber)
{
if (_repository.QueryCatalogNumberExists(catalogNumber))
{
return Json(true, JsonRequestBehavior.AllowGet);
}
return Json(false, JsonRequestBehavior.AllowGet);
}
我相信您还可以有其他字段,以便您可以检查目录号对于给定的 TeamCode 是否有效(我认为 TeamCode 必须可以为空,因为用户可能无法在输入目录号之前输入当前模型作为团队代码不是必需的)。所以你的模型是:
public class Line
{
[Remote("QueryCatalogNumberExistsForTeamCode", "Home", AdditionalFields = "TeamCode")]
public int CatalogNumber { get; set; }
public int TeamCode { get; set; }
}
和控制器代码:
public JsonResult QueryCatalogNumberExistsForTeamCode(int catalogNumber, int? teamCode)
{
if (_repository.QueryCatalogNumberExistsForTeamCode(catalogNumber, teamCode))
{
return Json(true, JsonRequestBehavior.AllowGet);
}
return Json(false, JsonRequestBehavior.AllowGet);
}
我希望这会为您指明解决方案的正确方向。