-1

I'm trying to use Data Annotations in my MVC project.

I've created my model and added all the appropriate annotations. I have my view and controller and tried so many ways but i didn't get any result.

When I click on the submit button the validation fired but error messaged not displaying to resolve it.

Model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace AllExamples.DTO
{
    public class EmployeeViewModel
    {
        public List<EmployeeInfo> objemployeeinfoList { get; set; }
        public EmployeeInfo objemployeeinfo { get; set; }
    }


    public class EmployeeInfo
    {
        public int EmployeeID { get; set; }
        [Display(Name = "Employee name")]
        [Required (ErrorMessage ="Employee name required.")]
        public string EmployeeName { get; set; }
        [Required(ErrorMessage = "Email id required.")]
        public string EmailID { get; set; }
        [Required(ErrorMessage = "Contact Number required.")]
        public string ContactNumber { get; set; }
        public string Department { get; set; }
        public string EmployeeType { get; set; }
        public string Roles { get; set; }
    }

    public class BillingInfo
    {
        public int BillingID { get; set; }
        public string BillingName { get; set; }
    }

    public class NonBillingInfo
    {
        public int nonbillingId { get; set; }
        public string Nonbillingname { get; set; }
    }
}

Index.cshtml

@model AllExamples.DTO.EmployeeViewModel
@{
    ViewBag.Title = "Home Page";
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @*@Html.AntiForgeryToken()*@

    @Html.ValidationSummary(false)

    <div class="form-group">
        @Html.Label("Employee Name", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.objemployeeinfo.EmployeeName, new { @class = "form-control", Name = "EmployeeName" })
            @Html.ValidationMessageFor(m => m.objemployeeinfo.EmployeeName) 
        </div>
    </div>
    <div class="form-group">
        @Html.Label("Email ID", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.objemployeeinfo.EmailID, new { @class = "form-control",Name= "EmailID" })
            @Html.ValidationMessageFor(m => m.objemployeeinfo.EmailID)
        </div>
    </div>
    <div class="form-group">
        @Html.Label("Contact Number", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.objemployeeinfo.ContactNumber, new { @class = "form-control", Name = "ContactNumber" })
            @Html.ValidationMessageFor(m => m.objemployeeinfo.ContactNumber)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Save" />
        </div>
    </div>
}

web.config

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

Can any one please help how to resolve this issue im not getting what i have missed?

RPichioli
  • 3,245
  • 2
  • 25
  • 29
Anil kumar
  • 121
  • 1
  • 1
  • 6
  • Because `new { Name = "EmployeeName"` }` is changing the `name` attribute of your form controls so they no longer match your model of the validation message placeholder generated by `ValidationMessageFor()`. **NEVER** attempt to change the `name` attribute when using the `HtmlHelper` methods. And view model do not contain data models - [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Oct 09 '17 at 21:57
  • Thank you very much it's working now. – Anil kumar Oct 10 '17 at 05:01
  • Do not accept incorrect answers - it just misleads other users. –  Oct 10 '17 at 22:33

2 Answers2

1

Since you are using a single employee info you don't have to use EmployeeViewModel instead you can use EmployeeInfo and pass the same from controller to view.

Change(View):

@model AllExamples.DTO.EmployeeInfo
@{
    ViewBag.Title = "Home Page";
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @*@Html.AntiForgeryToken()*@

    @Html.ValidationSummary(false)

    <div class="form-group">
        @Html.Label("Employee Name", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.EmployeeName, new { @class = "form-control", Name = "EmployeeName" })
            @Html.ValidationMessageFor(m => m.EmployeeName) 
        </div>
    </div>
    <div class="form-group">
        @Html.Label("Email ID", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.EmailID, new { @class = "form-control",Name= "EmailID" })
            @Html.ValidationMessageFor(m => m.EmailID)
        </div>
    </div>
    <div class="form-group">
        @Html.Label("Contact Number", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.ContactNumber, new { @class = "form-control", Name = "ContactNumber" })
            @Html.ValidationMessageFor(m => m.ContactNumber)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Save" />
        </div>
    </div>
}
Jibin Balachandran
  • 3,381
  • 1
  • 24
  • 38
  • if i changed like that .. i can't use multiple models in single view ..actually i have partial view in *Index.cshtml* view. for that I'm using EmployeeViewModel .. based on your response once i will change then how can display list of employees in table using partial view – Anil kumar Oct 10 '17 at 10:24
  • For displaying list of employees use `EmployeeViewModel` and for displaying details of single user use `EmployeeInfo`. – Jibin Balachandran Oct 10 '17 at 11:18
-1

Probably you are not using ModelState.IsValid in you code after post. Try it as

public ActionResult ControllerName(EmployeeInfo obj)
{
    if (ModelState.IsValid)
    {
        //// Your Code 
    }
}
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Manish Kumar
  • 184
  • 2
  • 9
  • This is a comment and a guess, not an answer – Ashley Medway Oct 09 '17 at 12:48
  • @AshleyMedway... I have asked controller code and also suggested the way that can be the solution. One more thing, not having right to comment that is why updated as an answer. Just giving a negative sign doesn't make sense. – Manish Kumar Oct 09 '17 at 12:53
  • Read the rules, because you don't have the permission doesn't mean you do have the permission to write a comment as answer. You are asking for more detail, this is a comment. read: https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead – Ashley Medway Oct 09 '17 at 12:55
  • [HttpPost] public ActionResult Index(EmployeeInfo objemp) { if (ModelState.IsValid) { } return View(); } – Anil kumar Oct 09 '17 at 13:04
  • I'm also using ModelState.IsValid in controller – Anil kumar Oct 09 '17 at 13:08
  • are you passing EmployeeInfo in your Post Parameter as in your view part you are using EmployeeViewModel. can you check on that – Manish Kumar Oct 09 '17 at 13:10