I have a database with 3 tables: -Pro_Process _Def_Defect _Cau_Cause
And I need to create a validate in the create function to see if the process code, for example, already exists. If it does show an error, if it does not let write, for this I used in the controller:
' GET: PRO_PROCESS/Create
Function Create() As ActionResult
Return View()
End Function
Public Function Available_Pro(CODE_PRO As String) As JsonResult
Return Json(Not db.PRO_PROCESS.Any(Function(c) c.CODE_PRO = CODE_PRO), JsonRequestBehavior.AllowGet)
End Function
' POST: PRO_PROCESS/Create
'To protect from overposting attacks, please enable the specific properties you want to bind to, for
'more details see http://go.microsoft.com/fwlink/?LinkId=317598.
<HttpPost()>
<ValidateAntiForgeryToken()>
Function Create(<Bind(Include:="CODE_PRO,DESCRIPTION_PRO")> ByVal pRO_PROCESS As PRO_PROCESS) As ActionResult
If ModelState.IsValid Then
db.PRO_PROCESS.Add(pRO_PROCESS)
db.SaveChanges()
Return RedirectToAction("Index")
End If
Return View(pRO_PROCESS)
End Function
In Model i have this
Partial Public Class PRO_PROCESS <Required(ErrorMessage:="Please, enter a valid code process")> <StringLength(6, ErrorMessage:="Process code must have only {1} characters.")> <Remote("Available_Pro", "PRO_PROCESS", ErrorMessage:="The inserted Process code already exists, please try a new one")> Public Property CODE_PRO As String <Required(ErrorMessage:="Description field can't be empty!")> Public Property DESCRIPTION_PRO As String Public Overridable Property DEF_DEFECT As ICollection(Of DEF_DEFECT) = New HashSet(Of DEF_DEFECT) End Class
It turns out that in the create function it works exactly as I wanted, but in the edit function it does not let me edit a record. Anyone know how I can overcome this problem?
here you can see the image of what happens to me