Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ValidationMessageFor in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 360
    Comment on it

    In MVC doing the validations on the client end and the server end you need to use the data annotations for it.

     

     

    So for doing the validation we also need to put it in the view in which we want to validate the controls.

     

     

    First we will put that validation on the model that we have made then we will put a control for it.

     

    public enum getstatus
        {
            success, failure
        }
        public class EmployeeModel
        {
    
            public int EmpId { get; set; }
            [Required(ErrorMessage = "First Name is required")]
            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            [Required(ErrorMessage = "Last Name is required")]
            public string LastName { get; set; }
            [Required(ErrorMessage = "PhoneNo is required")]
            public string PhoneNo { get; set; }
            public string Address { get; set; }
            public getstatus Status { get; set; }
            public string msg { get; set; }
        }

     

     

    After this we will put the validation control in the view for providing validations.

     

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        <div class="col-md-10">
            @Html.HiddenFor(model => model.EmpId, new { htmlAttributes = new { @class = "form-control" },id="hddnid" })
        </div>
    </div>
    
    <div class="form-group">
        @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" }, id = "txtfirstname" })
            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
        </div>
    </div>
    
    <div class="form-group">
        @Html.LabelFor(model => model.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.MiddleName, new { htmlAttributes = new { @class = "form-control", id = "txtmiddlename" } })
            @Html.ValidationMessageFor(model => model.MiddleName, "", new { @class = "text-danger" })
        </div>
    </div>
    
    <div class="form-group">
        @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" }, id = "txtlastname" })
            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
        </div>
    </div>
    
    <div class="form-group">
        @Html.LabelFor(model => model.PhoneNo, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PhoneNo, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PhoneNo, "", new { @class = "text-danger" })
        </div>
    </div>
    
    <div class="form-group">
        @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
        </div>
    </div>
    
    <div class="form-group">
        @Html.LabelFor(model => model.msg, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.msg, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.msg, "", new { @class = "text-danger" })
        </div>
    </div>
    
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
    
            @Html.ActionLink("Back to List", "Index")
        </div>
    
    
    
    
    </div>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: