2017-10-09 93 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'的详细信息。 –