Sunday, 15 October 2017

Pass (Show) Exception Error Message from Controller to View in ASP.Net MVC

Pass (Show) Exception Error Message from Controller to View in ASP.Net MVC


Description:

In this example we explain that how to Pass Exception error message from controller to view in Asp.Net MVC.or how to pass or send Exception error message from controller to Razor view in MVC.or how to show Exception error message from controller to view in Asp.Net MVC.or how to validate the control at controller side and display error message in Razor view in mvc.or how to validate the control validation in controller and pass this validation message to view and display it in validation summary.

Here we use Html.ValidationSummary Html Helper class to display Exception error message from controller to view in Asp.net MVC.
Model:

public class PersonModel
    {
        [Display(Name = "Age")]
        [Required(ErrorMessage = "Age is required.")]
        public string Age { get; set; }
    }

Controller:

public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(PersonModel person)
        {
            try
            {
                int age = int.Parse(person.Age);
            }
            catch (Exception ex)
            {
                string message = string.Format("<b>Message:</b> {0}<br /><br />", ex.Message);
                message += string.Format("<b>StackTrace:</b> {0}<br /><br />", ex.StackTrace.Replace(Environment.NewLine, string.Empty));
                message += string.Format("<b>Source:</b> {0}<br /><br />", ex.Source.Replace(Environment.NewLine, string.Empty));
                message += string.Format("<b>TargetSite:</b> {0}", ex.TargetSite.ToString().Replace(Environment.NewLine, string.Empty));
                ModelState.AddModelError(string.Empty, message);
            }

            return View();
        }
    }

View: 

@model MobileNumber_Validation_MVC.Models.PersonModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Pass (Show) Exception Error Message from Controller to View in ASP.Net MVC</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        }
        .error {
            color: red;
        }
        .exception {
            color: brown;
        }
    </style>
</head>
<body>
    @using (Html.BeginForm("Index", "Employee", FormMethod.Post))
    {
        <table>
            <tr>
                <td>@Html.LabelFor(m => m.Age)</td>
                <td>@Html.TextBoxFor(m => m.Age)</td>
                <td>@Html.ValidationMessageFor(m => m.Age, "",new { @class = "error" })</td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit"/></td>
                <td></td>
            </tr>
        </table>
        @Html.Raw(HttpUtility.HtmlDecode((Html.ValidationSummary(false, "", new { @class = "exception" })).ToHtmlString()))
    }
</body>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
</html>


This entry was posted in :

0 comments:

Post a Comment