How to display Exception (type) pass into AddModelError?
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; }
}
asp.net-mvc addmodelerror
add a comment |
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; }
}
asp.net-mvc addmodelerror
ValidationSummary()
only uses theErrorMessage
property of eachModelError
, it does not read theException
property. You can view the source code here - (I'm not sure what the point of that overload is since it does not appear to be used anywhere)
– user3559349
Nov 23 '18 at 10:09
add a comment |
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; }
}
asp.net-mvc addmodelerror
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; }
}
asp.net-mvc addmodelerror
asp.net-mvc addmodelerror
edited Nov 23 '18 at 9:33
Pop
asked Nov 23 '18 at 9:19
PopPop
14710
14710
ValidationSummary()
only uses theErrorMessage
property of eachModelError
, it does not read theException
property. You can view the source code here - (I'm not sure what the point of that overload is since it does not appear to be used anywhere)
– user3559349
Nov 23 '18 at 10:09
add a comment |
ValidationSummary()
only uses theErrorMessage
property of eachModelError
, it does not read theException
property. You can view the source code here - (I'm not sure what the point of that overload is since it does not appear to be used anywhere)
– user3559349
Nov 23 '18 at 10:09
ValidationSummary()
only uses the ErrorMessage
property of each ModelError
, it does not read the Exception
property. You can view the source code here - (I'm not sure what the point of that overload is since it does not appear to be used anywhere)– user3559349
Nov 23 '18 at 10:09
ValidationSummary()
only uses the ErrorMessage
property of each ModelError
, it does not read the Exception
property. You can view the source code here - (I'm not sure what the point of that overload is since it does not appear to be used anywhere)– user3559349
Nov 23 '18 at 10:09
add a comment |
1 Answer
1
active
oldest
votes
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);
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443731%2fhow-to-display-exception-type-pass-into-addmodelerror%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
}
}
add a comment |
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);
}
}
add a comment |
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);
}
}
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);
}
}
answered Nov 23 '18 at 11:01
scgoughscgough
2,96611632
2,96611632
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443731%2fhow-to-display-exception-type-pass-into-addmodelerror%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
ValidationSummary()
only uses theErrorMessage
property of eachModelError
, it does not read theException
property. You can view the source code here - (I'm not sure what the point of that overload is since it does not appear to be used anywhere)– user3559349
Nov 23 '18 at 10:09