ValidationSummary only display it if I pass in exception.Message
.
it display nothing if I pass in exception
.
But AddModelError
accept Exception type.
How do I display Exception
?
cshtml:
@model ControlTower2.Models.ViewModelUploadRawMaterial
@{
ViewBag.Title = "UploadRawMaterialSupplierData";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>UploadRawMaterialSupplierData</h2>
<div>
@using (Html.BeginForm("UploadRawMaterialSupplierData", "PurchaseOrder", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr>
<td>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(model => model.uploadFile, "", new { type = "file" })
</td>
<td>
@Html.ValidationMessageFor(model => model.uploadFile, null, new { @class = "text-danger" })
</td>
</tr>
<tr>
<td>
<input type="submit" value="Upload" />
</td>
</tr>
</table>
}
</div>
ActionResult:
[HttpPost]
public ActionResult UploadRawMaterialSupplierData(ViewModelUploadRawMaterial viewModelUploadRawMaterial)
{
try
{
throw new Exception("test UploadRawMaterialSupplierData error!");
}
catch (Exception exception)
{
ModelState.AddModelError("", exception);
return View(viewModelUploadRawMaterial);
}
}
View Model:
public class ViewModelUploadRawMaterial
{
[Required(ErrorMessageResourceType = typeof(Resources.UploadPurchaseOrder), ErrorMessageResourceName = "errorUploadFileRequired")]
public HttpPostedFileBase uploadFile { get; set; }
public List<UploadExcelError> UploadExcelErrors { get; set; }
}
ModelState.AddModelError()
accepts astring
value. It is primarily used to display a friendly error on-screen for the user to see something went wrong.You could analyse the exception and add an additional 'internal code' of your own devising that the user could quote to you to help you investigate any issues maybe?
e.g. Sorry there was a problem completing your action [ERR:1234] (where
1234
is your internal reference for something).Alternatively, if you want to output the whole Exception regardless of UX, you could install
Newtonsoft JSON.NET
package via Nuget and serialize the Exception to a string passing that through like this: