Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...
Posted
Updated 28-Dec-20 22:29pm
v2

1 solution

In the View Add Hidden field
C#
@Html.Hidden(initialProductCode, Model.ProductCode)


In the Remote attribute Add AdditionalFields
C#
[Remote("ValidateProductcode", "Controller", ErrorMessage = "Product Code already exists.", AdditionalFields = "initialProductcode")]


and the Controller action function
C#
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);
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900