ValidationSummary 仅在我传入exception.Message
.
如果我通过,它什么也不显示exception
。
但AddModelError
接受 Exception 类型。
如何显示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>
行动结果:
[HttpPost]
public ActionResult UploadRawMaterialSupplierData(ViewModelUploadRawMaterial viewModelUploadRawMaterial)
{
try
{
throw new Exception("test UploadRawMaterialSupplierData error!");
}
catch (Exception exception)
{
ModelState.AddModelError("", exception);
return View(viewModelUploadRawMaterial);
}
}
查看型号:
public class ViewModelUploadRawMaterial
{
[Required(ErrorMessageResourceType = typeof(Resources.UploadPurchaseOrder), ErrorMessageResourceName = "errorUploadFileRequired")]
public HttpPostedFileBase uploadFile { get; set; }
public List<UploadExcelError> UploadExcelErrors { get; set; }
}