我想在我的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) –
非常感謝你它的工作現在。 –
不要接受錯誤的答案 - 它只會誤導其他用戶。 –