Exception Handling at MODEL

Asked

Viewed 58 times

0

How do I send an exception of the Model to View in the form of an Alert, for example MODEL :

Try { 
    // codigo 

}catch{

    // Aqui quero tratar o erro e mandar uma mensagem pra ser exibida na view em forma de alerta.

}

Thank you.

2 answers

1


If you find a problem with the model, you can send an Alert like this:

public ActionResult Create(ItemModel fvm){
   try
   {

   } 
   catch (Exception e)
   {
      TempData["msg"] = "<script>alert('Olá mundo');</script>";
   }
}

@Html.Raw(TempData["msg"])

or

return JavaScript(alert("Olá mundo"));

or

return Content("<script language='javascript' type='text/javascript'>alert('Olá mundo');</script>");

0

You can use the AddModelError. With it, an Alert will not be displayed with the error message, but the error will be displayed on the screen equal error handling which are made in inputs.

On your controller:

Try { 
    // codigo 

}catch{

     ModelState.AddModelError("", "Coloque seu erro aqui.");
}

In the view, you use @Html.ValidationSummary(true, "", new { @class = "text-danger" }) for the error to be displayed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.