Monday, 16 October 2017

Add (Apply) CSS Class (Styles) in Html.ValidationMessageFor method in ASP.Net MVC

Add (Apply) CSS Class (Styles) in Html.ValidationMessageFor method in ASP.Net MVC
Description:

In this example we explain that how to add CSS style in Html.ValidationMessageFor method in Asp.Net MVC.or how to apply CSS class in validation message in MVC.or how to apply style in validation message in MVC.

So how to add CSS style or CSS class in ValidationMessageFor method in MVC.

Model:

    public class EmployeeModel
    {
        [Required(ErrorMessage = "Employee Name is required.")]
        public string EmployeeName { get; set; }

   }

Controller:

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

        [HttpPost]
        public ActionResult Index(EmployeeModel emp)
        {
            return View();
        }
    }

 View:

@model TextBox_Validation_MVC.Models.PersonModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>ASP.Net MVC: Add (Apply) CSS Class (Styles) in Html.ValidationMessageFor method</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        }
        .error {
            color: red;
        }
    </style>
</head>
<body>
    @using (@Html.BeginForm("Index", "Employee", FormMethod.Post))
    {
        <table>
            <tr>
                <td>@Html.DisplayFor(m => m.EmployeeName)</td>
                <td>@Html.TextBoxFor(m => m.EmployeeName)</td>
                <td>@Html.ValidationMessageFor(m => m.EmployeeName, "", new { @class = "error" })</td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit"/></td>
                <td></td>
            </tr>
        </table>
    }
</body>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
</html>


This entry was posted in :

0 comments:

Post a Comment