Getting the Error Description from ModelState
-
Hi, I'm using a FormView and update method which try to update the database, but it's throwing and error, I need to know on which field the error was but I am not able to extract the error msg from the ModelStateDictionary so I can know what is the exact error msg, the code as follow:
Public Function UpdateTest(id As Integer) As Integer
Dim Employee = _db.DbSet_Employees.Find(id)Try If Employee Is Nothing Then ModelState.AddModelError("ModelError", String.Format("Employee with ID {0} could not be found", id)) Return 0 End If TryUpdateModel(Employee) If Not ModelState.IsValid Then ModelState.AddModelError("ModelError", String.Format("Employee with ID {0} could not be updated", id)) MsgBox(ModelState.GetErrorMessageForKey("error")) Return 0 End If Catch ex As Exception ' Me.lblMsg.Text = Err.Description.ToString Me.lblMsg2.Text = ex.Message.ToString Return 0 End Try ' write the changes to the database Return \_db.SaveChanges End Function
Public Module ExtMethod
<System.Runtime.CompilerServices.Extension> _
Public Function GetErrorMessageForKey(ByVal dictionary As ModelStateDictionary, ByVal key As String) As String
Return dictionary(key).Errors.First().ErrorMessage
End Function
End ModuleThe problem start from TryUpdateModel then in the temperrory line MsgBox() Thanks for the help.
-
Hi, I'm using a FormView and update method which try to update the database, but it's throwing and error, I need to know on which field the error was but I am not able to extract the error msg from the ModelStateDictionary so I can know what is the exact error msg, the code as follow:
Public Function UpdateTest(id As Integer) As Integer
Dim Employee = _db.DbSet_Employees.Find(id)Try If Employee Is Nothing Then ModelState.AddModelError("ModelError", String.Format("Employee with ID {0} could not be found", id)) Return 0 End If TryUpdateModel(Employee) If Not ModelState.IsValid Then ModelState.AddModelError("ModelError", String.Format("Employee with ID {0} could not be updated", id)) MsgBox(ModelState.GetErrorMessageForKey("error")) Return 0 End If Catch ex As Exception ' Me.lblMsg.Text = Err.Description.ToString Me.lblMsg2.Text = ex.Message.ToString Return 0 End Try ' write the changes to the database Return \_db.SaveChanges End Function
Public Module ExtMethod
<System.Runtime.CompilerServices.Extension> _
Public Function GetErrorMessageForKey(ByVal dictionary As ModelStateDictionary, ByVal key As String) As String
Return dictionary(key).Errors.First().ErrorMessage
End Function
End ModuleThe problem start from TryUpdateModel then in the temperrory line MsgBox() Thanks for the help.
I've done the following, created a model & then a function to be able to use it around my app:
Public Function GetErrorList(ByVal modelState As ModelStateDictionary) As String
Dim errorList = ( _
From item In modelState _
Where item.Value.Errors.Any() _
Select item.Value.Errors(0).ErrorMessage).ToList()Return "Error in Rec. {0}: " & errorList.Item(0).ToString
End Function
Then I call the function:
TryUpdateModel(Customer)
If Not ModelState.IsValid Then
ModelState.AddModelError("ModelError", String.Format(GetErrorList(ModelState), id))
Return 0
End If