I have used model validation in my application also i have used razor rendering engine. When i used to insert values from my form it doesnt validate?? also i have a table in my database also i have a column name "code" in my database which is of type varchar.
I have to insert unique code for every department then how to validate it? also tips for server validation. please do help me out.
Marked as answer by brajeshkarna on Feb 19, 2013 10:04 AM
Have you decorated the data validation annotations on Model, that would be easiest way of implementing the validation. Regarding the unique code validation, you can write your custom validation attribute which can validate entered date for uniqueness.
yup i did checked model.isvalid . I think the problem is it doesnt load the jquery coz i m using mvc 4 and the queries are bundled. while throwing the validation to the controller it works.
could u please help me out with validation from database:
i have a column which has certain codes (varchar) and when inserting a new record the code must be unique and my application should be able to check about the uniqueness of the code.
brajeshkarna
Member
6 Points
14 Posts
validation in asp.net MVC
Feb 08, 2013 06:48 AM|LINK
I have used model validation in my application also i have used razor rendering engine. When i used to insert values from my form it doesnt validate?? also i have a table in my database also i have a column name "code" in my database which is of type varchar. I have to insert unique code for every department then how to validate it? also tips for server validation. please do help me out.
goel.ankit
Contributor
2531 Points
513 Posts
Re: validation in asp.net MVC
Feb 08, 2013 06:59 AM|LINK
Have you decorated the data validation annotations on Model, that would be easiest way of implementing the validation. Regarding the unique code validation, you can write your custom validation attribute which can validate entered date for uniqueness.
http://www.dotnetcurry.com/ShowArticle.aspx?ID=776
http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvccustomvalidation.aspx
Please share your code of validation to find out the issue.
Ankit
(Please select 'Mark as Answer' if my response has helped you.)
brajeshkarna
Member
6 Points
14 Posts
Re: validation in asp.net MVC
Feb 08, 2013 07:05 AM|LINK
Dear Ankit I have decorated the data validation annotations on model. I worked out for all the possibilities but didnt worked out..
Alsaady
Participant
1736 Points
357 Posts
Re: validation in asp.net MVC
Feb 08, 2013 07:11 AM|LINK
I think you can through stored procedure and make validateas
Error varchar(50) output
if exest (.........)
error ="This found "
......
this idea
www.rtoosh.net
goel.ankit
Contributor
2531 Points
513 Posts
Re: validation in asp.net MVC
Feb 08, 2013 08:28 AM|LINK
Are you checking Model.IsValid in your controller before updating database. Can you please share your sample code to find out the exact issue.
Ankit
(Please select 'Mark as Answer' if my response has helped you.)
brajeshkarna
Member
6 Points
14 Posts
Re: validation in asp.net MVC
Feb 08, 2013 08:34 AM|LINK
yup i did checked model.isvalid . I think the problem is it doesnt load the jquery coz i m using mvc 4 and the queries are bundled. while throwing the validation to the controller it works.
could u please help me out with validation from database:
i have a column which has certain codes (varchar) and when inserting a new record the code must be unique and my application should be able to check about the uniqueness of the code.
Ramanghantiy...
Member
174 Points
56 Posts
Re: validation in asp.net MVC
Feb 08, 2013 12:48 PM|LINK
hi brajeshkarna
please go throw it
http://www.codeproject.com/Articles/422573/Model-Validation-in-ASP-NET-MVC
if not resolve feel free mail me.
Raman Ghantiyala
Asp.net Developer
brajeshkarna
Member
6 Points
14 Posts
Re: validation in asp.net MVC
Feb 13, 2013 03:15 AM|LINK
I used "Remote" in my modal class and passed the action as well as the controller and returned the json..
I did it like this
in my model i used like this
[Remote("ValidateCode", "LedgerAttributeCode", ErrorMessage = "Code Already Exist")]
public string Code { get; set; }
in my controller i used
public ActionResult ValidateCode(LedgerAttributeCodeModel getCode)
{
if (!service.ValidateCode(getCode.Code))
return Json(true, JsonRequestBehavior.AllowGet);
return Json(false, JsonRequestBehavior.AllowGet);
}
and in my service i used
public bool ValidateCode(string getCode)
{
foreach (LedgerAttributeCodeModel cod in GetLedgerAttributeCode())
{
if(string.Equals(cod.Code, getCode, StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
this helped me out...
But I have a problem again i.e. when javascript is disabled in browser it doesn't validate.... plz do help me out with this...