locked
MVC Custom validation when uploading Excel File using EPPLUS RRS feed

Answers

  • User1520731567 posted

    Hi shabbir_215,

    I suggest you could use @Html.ValidationSummary.

    You can judge in back end,check if Firstname and Lastname is empty in httppost action Upload.

    And then add ModelState.AddModelError write custom error message,

    Finally,pass error to @Html.ValidationSummary in the view.

    For example:

    public ActionResult Upload(FormCollection formCollection)
    {
       ....
    if(xxx)//Judging null
    {
    ModelState.AddModelError(string.Empty, "Firstname and Lastname are empty"); //Add error message if the condition is met

    }
    else
    {

    } return View("Index");//this will jump to Index View }

    And then in Index View,add @Html.ValidationSummary,like:

    ...
    <p>Index</p>
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })//this will receive error and display
    ...

    Like the picture:

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 8, 2019 7:12 AM

All replies

  • User1520731567 posted

    Hi shabbir_215,

    I suggest you could use @Html.ValidationSummary.

    You can judge in back end,check if Firstname and Lastname is empty in httppost action Upload.

    And then add ModelState.AddModelError write custom error message,

    Finally,pass error to @Html.ValidationSummary in the view.

    For example:

    public ActionResult Upload(FormCollection formCollection)
    {
       ....
    if(xxx)//Judging null
    {
    ModelState.AddModelError(string.Empty, "Firstname and Lastname are empty"); //Add error message if the condition is met

    }
    else
    {

    } return View("Index");//this will jump to Index View }

    And then in Index View,add @Html.ValidationSummary,like:

    ...
    <p>Index</p>
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })//this will receive error and display
    ...

    Like the picture:

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 8, 2019 7:12 AM
  • User-1256377279 posted

    Thanks Yuki

    Tuesday, January 8, 2019 11:02 AM