I had to do some kind of duplication check of data when creating an entry (lets say username). I did it by this. (It's in VB, but don't bother)
In controller:
Function CheckForDuplication(ByVal UserName As String) As JsonResult
If db.Users.Count(Function(x) x.Username.ToLower() = UserName.ToLower()) > 0 Then
Return Json(False, JsonRequestBehavior.AllowGet)
Else
Return Json(True, JsonRequestBehavior.AllowGet)
End If
End Function
and in model:
<Remote("CheckForDuplication", "User", HttpMethod:="POST", ErrorMessage:="username not available")>
Public Property Username As String
and using standard JS validation libraries in view.
It works real fine... when creating. The trouble is that when editing you can't save back the same username because it's forbidden be the client-side validation.. as it already exist in DB. How can the validation be over-passed for current 'username' but remain for others. Or it should be redone with custom JS and [remote] removed from model. Thank you