. net MVC5 -验证输入的字符串是否已经存在于json文件中
本文关键字:存在 json 文件 是否 MVC5 验证 输入 字符串 net | 更新日期: 2023-09-27 18:16:51
如果用户试图输入已存在于列表中的Product Name以避免重复条目,如何显示"对不起,此产品已存在"的消息?我要检查的字符串在名为TechItem.cs
的JSON文件中的Product中调用。这是我到目前为止所尝试的…
在我的模型中
[Required]
[Remote("doesProductExist", "TechEditor", HttpMethod = "POST", ErrorMessage = "This Product already exists within the Quadrant. Please enter a different Product or Go Back and Remove this Product from the List.")]
public string Product { get; set; }
和控制器中的
[HttpPost]
public JsonResult doesProductExist(string Product)
{
var product = (Product);
return Json(product == null);
}
任何想法吗?目前,只要您在该文本框中输入任何内容,该消息就会显示。
为控制器添加以下方法:
[AllowAnonymous]
public async Task<JsonResult> doesProductExist(string Product)
{
var result =
await userManager.FindByNameAsync(Product) ??
await userManager.FindByEmailAsync(Product);
return Json(result == null, JsonRequestBehavior.AllowGet);
}