Reputation: 12988
I have a view with a upload controller wich fires this ActionResult:
[HttpPost]
public ActionResult ProcessSubmitUpload(HttpPostedFileBase attachments, Guid? idDocument)
{
//Validations
var xmlDocument = XDocument.Load(attachments.InputStream);
DocumentCommonHelper.SendFile(xmlDocument);
}
The SendFile method:
public static void SendCte(XDocument xmlDocument)
{
var client = new WsSoapClient();
client.InsertXML(xmlDocument);
}
The webservice returns SoapException if somethig goes wrong, like: "The IDDocument does not exist" , etc etc.
But in MVC when im debugging if something goes wrong in the InsertXml method i cannot catch it, the debug navigation just stop and throw a Upload File error.
I would like to catch the return message of the InsertXML method in my action e return it to my view. Like a 'alert' popup. How can i do that?
Upvotes: 2
Views: 3204
Reputation: 5751
You can return the error message to the view in the ModelState object. The view can display it using a ValidationSummary helper:
[HttpPost]
public ActionResult ProcessSubmitUpload(HttpPostedFileBase attachments, Guid? idDocument)
{
//Validations
var xmlDocument = XDocument.Load(attachments.InputStream);
try
{
DocumentCommonHelper.SendFile(xmlDocument);
}
catch(Exception ex)
{
ModelState.AddModelError("ProcessSubmitUpload", ex.Message);
return View(new MyViewModel())
}
}
View:
@using(Html.BeginForm("ProcessSubmitUpload", "MyController", FormMethod.Post))
{
@Html.ValidationSummary()
... etc.
}
Upvotes: 3
Reputation: 3380
You can wrap your SendFile
method call with try..catch
block and it catch
section fill return result with appropriate information about fault.
Upvotes: 2