2017-10-09 72 views
-1

我想在我的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解決這個問題我沒有得到我錯過了什麼?

+0

因爲'新{名稱= 「EmployeeName」'}'是改變'你的表單控件的屬性name',使他們不再滿足您的驗證模型由'ValidationMessageFor()'生成的消息佔位符。 **從不嘗試在使用'HtmlHelper'方法時更改'name'屬性。和視圖模型不包含數據模型 - [什麼是視圖模型在MVC(https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –

+0

非常感謝你它的工作現在。 –

+0

不要接受錯誤的答案 - 它只會誤導其他用戶。 –

回答

-1

也許你不交後,你的代碼使用ModelState.IsValid。嘗試,因爲

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

這是一條評論和一個猜測,而不是一個答案 –

+0

@AshleyMedway ...我問過控制器代碼,並提出瞭解決方案。還有一件事,沒有評論的權利,這就是爲什麼更新爲答案。只是給一個負面信號沒有意義。 –

+0

閱讀規則,因爲你沒有權限並不意味着你有權寫評論作爲答案。您正在詢問更多詳情,這是一條評論。閱讀:https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an-i-do-instead –

1

由於您使用的是一個員工的信息,你不必使用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> 
} 
+0

..我不能在單一視圖中使用多個模型..實際上,我在* Index.cshtml *視圖中有部分視圖。爲此我使用EmployeeViewModel ..根據你的反應,一旦我將改變,那麼如何使用局部視圖顯示錶中的員工列表 –

+0

用於顯示員工列表使用'EmployeeViewModel'並顯示單個用戶使用'EmployeeInfo'的詳細信息。 –