我想在我的MVC项目中使用数据注解。mvc中的数据注释
我已经建立了我的模型,并添加了所有适当的注解。我有我的看法和控制器,并尝试了很多方法,但我没有得到任何结果。
当我点击提交按钮确认发射,但错误不传递消息显示解决它。
型号
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" />
任何一个可以请帮助^ h解决这个问题我没有得到我错过了什么?
因为'新{名称= “EmployeeName”'}'是改变'你的表单控件的属性name',使他们不再满足您的验证模型由'ValidationMessageFor()'生成的消息占位符。 **从不尝试在使用'HtmlHelper'方法时更改'name'属性。和视图模型不包含数据模型 - [什么是视图模型在MVC(https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –
非常感谢你它的工作现在。 –
不要接受错误的答案 - 它只会误导其他用户。 –