1

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

https://pt.stackoverflow.com/questions/257629/%C3%89-possivel-anular-o-comando-remote-apenas-em-editar

Somebody
  • 11
  • 5
  • Use 2 different view models for the `Edit` and `Create` actions (one with and one without the `[Remote]` attribute. **Do not use data models in your view!** Or use the `AdditionalFields` property of the `RemoteAttribute` to pass the ID of the object which you ca test in the `Available_Pro` to determine if its a new or existing object. –  Nov 28 '17 at 09:02
  • @StephenMuecke how can i do that " Or use the AdditionalFields property of the RemoteAttribute to pass the ID of the object which you ca test in the Available_Pro to determine if its a new or existing object." can you please show me? – Somebody Nov 28 '17 at 09:05
  • See the dupe which explains how to use `AdditionalFields` to pass the `ID` of the object (which would be `null` in a `Create` method and have some value in an `Edit` methods. And strongly suggest you read [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Nov 28 '17 at 09:07
  • I don't have an ID field, i'm using the CODE_PRO(varchar), is that a problem? – Somebody Nov 28 '17 at 09:54
  • Are you telling me you have database tables that do not have a ID field that identifies the record? (if so, you better rethink what your doing) –  Nov 28 '17 at 09:57
  • I am a trainee, this application already had the database made when I was given to finish, I can not touch it, I have to adapt the situations to what they gave me, you see? It does not make sense to me, but I can not deal with it. – Somebody Nov 28 '17 at 10:09
  • The you need 2 separate view models and `EditViewModel` and ` CreateViewModel` containing the properties you need in the view (and you apply the attribute to only the `CreateViewModel` that is used in your `Create()` method. –  Nov 28 '17 at 10:11
  • thank you, you really help me :) – Somebody Nov 28 '17 at 10:13

0 Answers0