How to display Exception (type) pass into AddModelError?












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; }
}









share|improve this question

























  • 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
















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; }
}









share|improve this question

























  • 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














0












0








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; }
}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 9:33







Pop

















asked Nov 23 '18 at 9:19









PopPop

15010




15010













  • 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

















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












1 Answer
1






active

oldest

votes


















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);
}
}





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    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);
    }
    }





    share|improve this answer




























      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);
      }
      }





      share|improve this answer


























        0












        0








        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);
        }
        }





        share|improve this answer













        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);
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 11:01









        scgoughscgough

        2,98621632




        2,98621632
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga