Asp.net mvc mvc中的数据注释

Asp.net mvc mvc中的数据注释,asp.net-mvc,Asp.net Mvc,我试图在我的MVC项目中使用数据注释 我已经创建了我的模型并添加了所有适当的注释。我有我的视图和控制器,尝试了很多方法,但没有得到任何结果 当我单击submit按钮时,验证被触发,但错误消息没有显示以解决它 型号 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace Al

我试图在我的MVC项目中使用数据注释

我已经创建了我的模型并添加了所有适当的注释。我有我的视图和控制器,尝试了很多方法,但没有得到任何结果

当我单击submit按钮时,验证被触发,但错误消息没有显示以解决它

型号

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; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用System.Web;
名称空间AllExamples.DTO
{
公共类EmployeeViewModel
{
公共列表objemployeeinfoList{get;set;}
public EmployeeInfo objeemployeeinfo{get;set;}
}
公共类EmployeeInfo
{
public int EmployeeID{get;set;}
[显示(Name=“员工姓名”)]
[必需(ErrorMessage=“必需的员工姓名”)]
公共字符串EmployeeName{get;set;}
[必需(ErrorMessage=“电子邮件id必需。”)]
公共字符串EmailID{get;set;}
[必需(ErrorMessage=“必需的联系电话号码”)]
公共字符串ContactNumber{get;set;}
公共字符串部门{get;set;}
公共字符串EmployeeType{get;set;}
公共字符串角色{get;set;}
}
公共类计费信息
{
public int BillingID{get;set;}
公共字符串BillingName{get;set;}
}
公共类非BillingInfo
{
public int nonbillingId{get;set;}
公共字符串非BillingName{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>
}
@model AllExamples.DTO.EmployeeViewModel
@{
ViewBag.Title=“主页”;
}
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@*@Html.AntiForgeryToken()*@
@Html.ValidationSummary(false)
@Label(“雇员姓名”,新的{@class=“col-md-2控制标签”})
@TextBoxFor(m=>m.objeemployeeinfo.EmployeeName,新的{@class=“form control”,Name=“EmployeeName”})
@Html.ValidationMessageFor(m=>m.objeemployeeinfo.EmployeeName)
@Label(“电子邮件ID”,新的{@class=“col-md-2控制标签”})
@TextBoxFor(m=>m.objemployeeinfo.EmailID,新的{@class=“form control”,Name=“EmailID”})
@Html.ValidationMessageFor(m=>m.objemployeeinfo.EmailID)
@Label(“联系人号码”,新的{@class=“col-md-2控制标签”})
@Html.TextBoxFor(m=>m.objemployeeinfo.ContactNumber,新的{@class=“form control”,Name=“ContactNumber”})
@Html.ValidationMessageFor(m=>m.objemployeeinfo.ContactNumber)
}
web.config

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


有人能帮我解决这个问题吗?我没有得到我错过的东西。

可能你没有使用
ModelState.IsValid
,在你发布后的代码中。试一试

public ActionResult ControllerName(EmployeeInfo obj)
{
    if (ModelState.IsValid)
    {
        //// Your Code 
    }
}

由于您使用的是单个员工信息,因此不必使用
EmployeeViewModel
,而是可以使用
EmployeeInfo
并将其从控制器传递到视图

更改(视图):

@model AllExamples.DTO.EmployeeInfo
@{
ViewBag.Title=“主页”;
}
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@*@Html.AntiForgeryToken()*@
@Html.ValidationSummary(false)
@Label(“雇员姓名”,新的{@class=“col-md-2控制标签”})
@TextBoxFor(m=>m.EmployeeName,新的{@class=“form control”,Name=“EmployeeName”})
@Html.ValidationMessageFor(m=>m.EmployeeName)
@Label(“电子邮件ID”,新的{@class=“col-md-2控制标签”})
@TextBoxFor(m=>m.EmailID,新的{@class=“form control”,Name=“EmailID”})
@Html.ValidationMessageFor(m=>m.EmailID)
@Label(“联系人号码”,新的{@class=“col-md-2控制标签”})
@TextBoxFor(m=>m.ContactNumber,新的{@class=“form control”,Name=“ContactNumber”})
@Html.ValidationMessageFor(m=>m.ContactNumber)
}

这是一个评论和猜测,而不是一个猜测answer@AshleyMedway... 我询问了控制器代码,并提出了解决方案。还有一件事,没有评论权,这就是为什么更新为答案的原因。仅仅给出一个否定的符号是没有意义的。阅读规则,因为你没有权限并不意味着你有权限写评论作为答案。你要求的是更多的细节,这是一个评论。阅读:[HttpPost]公共操作结果索引(EmployeeInfo objemp){if(ModelState.IsValid){}返回视图();}我在controllerBecause
new{Name=“EmployeeName”
}中也使用ModelState.IsValid`正在更改表单控件的
name
属性,使它们不再匹配由
ValidationMessageFor()生成的验证消息占位符的模型。使用
HtmlHelper
方法时,切勿尝试更改
name
属性。视图模型不包含数据模型-非常感谢它现在正在工作。不要接受错误的答案-它只会误导其他用户。如果我这样更改。。我不能在一个视图中使用多个模型。实际上,我在Index.cshtml视图中有部分视图。为此,我使用EmployeeViewModel。。根据您的回答,我将更改如何使用部分视图在表中显示员工列表用于显示员工列表使用
EmployeeViewModel
和显示单个用户使用的详细信息
EmployeeInfo
@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>
}