0

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; }
}
Pop
  • 525
  • 1
  • 7
  • 22
  • `ValidationSummary()` only uses the `ErrorMessage` property of each `ModelError`, it does not read the `Exception` property. You can view the [source code here](https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc/Html/ValidationExtensions.cs) - (I'm not sure what the point of that overload is since it does not appear to be used anywhere) –  Nov 23 '18 at 10:09

1 Answers1

0

ModelState.AddModelError() accepts a string 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:

[HttpPost]
public ActionResult UploadRawMaterialSupplierData(ViewModelUploadRawMaterial viewModelUploadRawMaterial)
{
    try
    {
        throw new Exception("test UploadRawMaterialSupplierData error!");
    }
    catch (Exception exception)
    {
        string jsonException = JsonConvert.SerializeObject(exception);
        ModelState.AddModelError("", jsonException);
        return View(viewModelUploadRawMaterial);
    }
}
scgough
  • 5,099
  • 3
  • 30
  • 48