How can I use Remote Validation with entity framework Code First Approach in ASP .Net MVC.

Posted by Kumarkrishna184 under ASP.NET MVC on 9/26/2018 | Points: 10 | Views : 563 | Status : [Member] | Replies : 1
How can I use Remote Validation with entity framework Code First Approach in ASP .Net MVC. Where table name is CouponTab and the field on which I want to apply Remote validation on CouponCode Column. I do not want to insert coupon Code data repeated.



Thanks in Advance.....

Thanks and Regards,
Krishna Kumar



Responses

Posted by: Ajaygohil on: 3/18/2019 [Member] Starter | Points: 25

Up
0
Down
you can add in model this validation on attribute:-
public class CouponTab 
{
public int Id { get; set; }

[Required]
[Remote("IsCouponCodeExist", "Product",ErrorMessage = "Product name already exists")]
public string CouponCode { get; set; }
}

and add the validation method to controller like:-
public JsonResult IsCouponCodeExist(string CouponCode)
{
var validateCode = db.CouponTab .FirstOrDefault
(x => x.CouponCode == CouponCode);
if (validateName != null)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}

http://www.ifourtechnolab.com/



ASP.NET MVC Developer
iFour Technolab Private Limited
http://www.ifourtechnolab.com

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response