如何在MVC5中以编辑模式限制远程验证 [英] How to restrict the remote validation in Edit mode in MVC5
本文介绍了如何在MVC5中以编辑模式限制远程验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I have a Remote attribute on the ProductCode property of Product model. When I create a new Product it works perfect and tells the user that an ProductCode is already in use. Now I'm having a problem in my editing form (if the user wants to updates values). It tells the user that his/her ProductCode is already in use, I don't want it to give that message at edit mode . How can I restrict the Remote attribute to behave in Edit mode? i searched but didnt get proper solution...
推荐答案
在视图添加隐藏字段中
In the View Add Hidden field
@Html.Hidden(initialProductCode, Model.ProductCode)
在Remote属性中添加AdditionalFields
In the Remote attribute Add AdditionalFields
[Remote("ValidateProductcode", "Controller", ErrorMessage = "Product Code already exists.", AdditionalFields = "initialProductcode")]
和Controller动作函数
and the Controller action function
public JsonResult ValidateProductcode(string Productcode, string initialProductcode)
{
try
{
if (Productcode == initialProductcode)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
var Productcode = datacontext.tablename.Single(m => m.ProductCode == Productcode);
return Json(false, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
这篇关于如何在MVC5中以编辑模式限制远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文