2010-08-12 130 views
0

我有以下数据库表:在ASP.Net验证模型MVC2

alt text

这里是我使用来验证实体框架创建模型的代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 
using System.Web.Mvc; 

namespace UTEPSA.Models 
{ 
    [MetadataType(typeof(Area_Validation))] 
    public partial class Area 
    { 

    } 

    public class Area_Validation 
    { 
     [Required(ErrorMessage = "Campo requerido: Debe elegir un Jefe valido.")]   
     public int IDJefe { get; set; } 

     [Required(ErrorMessage = "Campo requerido: Nombre")] 
     public string Nombre { get; set; } 
    } 
} 

ID字段在int主键中,它是一个标识,所以它自动增量,我不必输入字段。

但是,当我尝试在编辑时为其保存表单时,我需要写入任何数字以供它通过。

When I leave the field blank: 
Validation fails. 

When I type in a string: 
Validation fails. 

When I type in ANY number: 
Validation passes. 

如何忽略这一领域的任何建议?

渲染HTML是:

<form action="/area/create" method="post"> 

     <fieldset> 
      <legend>Fields</legend> 

      <div class="editor-label"> 
       <label for="ID">ID</label> 
      </div> 
      <div class="editor-field"> 
       <input id="ID" name="ID" type="text" value="0" /> 

      </div> 

      <div class="editor-label"> 
       <label for="IDJefe">IDJefe</label> 
      </div> 
      <div class="editor-field"> 
       <input id="IDJefe" name="IDJefe" type="text" value="" /> 

      </div> 

      <div class="editor-label"> 
       <label for="Nombre">Nombre</label> 
      </div> 
      <div class="editor-field"> 
       <input id="Nombre" name="Nombre" type="text" value="" /> 

      </div> 

      <p> 
       <input type="submit" value="Create" /> 
      </p> 
     </fieldset> 

    </form> 

回答

-1

你不应该将其设置为必选的话,因为它不需要插件。