首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EF和DataBaseFirst不起作用的DataAnnotatons

问EF和DataBaseFirst不起作用的DataAnnotatons
EN

Stack Overflow用户
提问于 2011-09-10 03:06:40
回答 1查看 106关注 0票数 0

我已经用元数据修改了我的模型中的代码(它在一个单独的层上),希望这能解决这个问题,但它没有解决这个问题,就像模型中的DataAnnotations没有被提取一样?为什么?

在查看页面源代码时,我应该会看到类似以下内容:

代码语言:javascript
复制
data-val-required="The User name field is required."

然而,我所看到的只有以下几点:

代码语言:javascript
复制
<div class="editor-label">




            <label for="Email">Email</label>




        </div>




        <div class="editor-field">




            <input id="Email" name="Email" type="text" value="" />




            <span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>




        </div>

在查看标准帐户登录的模型时,我有以下几点:

代码语言:javascript
复制
YeagerTech.Models.LogOnModel

以上是在AccountModels.cs文件中网站根目录下的以下Models文件夹中定义的:

代码语言:javascript
复制
public class LogOnModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

上面(当模型在网站前端,而不是在另一个层中)运行良好,并且在页面源fine.What中设置了所需的属性,我需要做什么才能使用EF DataBaseFirst方法执行数据验证,而我的模型位于另一个层?我的web.config文件设置如下:

代码语言:javascript
复制
<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

在前端,我的视图设置如下:

代码语言:javascript
复制
@model YeagerTech.YeagerTechWcfService.Customer

@{
    ViewBag.Title = "Create Customer";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Ajax.BeginForm("Create", "Customer", new AjaxOptions { HttpMethod = "POST"}))
{
    @Html.ValidationSummary(true, "Create was unsuccessful. Please correct the errors and try again.") 
    <fieldset>
        <legend>Customer</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

YeagerTech.YeagerTechWcfService.Customer的定义设置如下(在Reference.cs文件中)

代码语言:javascript
复制
[System.Diagnostics.DebuggerStepThroughAttribute()]     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]     [System.Runtime.Serialization.DataContractAttribute(Name="Customer", Namespace="http://schemas.datacontract.org/2004/07/YeagerTechModel")]     [System.SerializableAttribute()]     public partial class Customer : YeagerTech.YeagerTechWcfService.CustomerDA {     }

我的模型在上面的服务中设置如下:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace YeagerTechModel
{
    [Serializable]
    [DataContract]
    [MetadataType(typeof(CustomerDA))]
    public partial class Customer : CustomerDA
    {
        public Customer()
        {
            this.Projects = new HashSet<Project>();
        }
    }

    [Serializable]
    [DataContract]
    public class CustomerDA 
    {  
        [DataMember]
        public short CustomerID { get; set; }

        [Required]
        [StringLength(50)]
        [DataType(DataType.EmailAddress)]
        [DataMember]
        public string Email { get; set; }

基本DbContext类

代码语言:javascript
复制
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace YeagerTechModel
{
    public partial class YeagerTechEntities : DbContext
    {
        public YeagerTechEntities()
            : base("name=YeagerTechEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Customer> Customers { get; set; }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2011-09-10 12:28:00

我从您的代码中看到,您包含了jquery.validate.js脚本和jquery.validate.unobtrusive.js script。但是,我没有看到jquery脚本被包括在内。必须包含jquery,并且必须是第一个脚本。此外,由于您使用的是MVC的Ajax功能,因此还需要包含jquery.unobtrusive-ajax.js脚本。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7366317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档