验证显示不出来使用EF code先用复杂类型 [英] Validations not show up using EF Code First with complex types

查看:122
本文介绍了验证显示不出来使用EF code先用复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这个问题的 Model类测绘

我有我的客户端类现在工作正常,它的定义为

I had my Client class now working fine and it's defined as

using System;

using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

using DataAnnotationsExtensions;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;

namespace CardNumbers.Objects
{
    [ComplexType]
    public class PhoneInfo
    {
        [DataType(DataType.PhoneNumber)]
        [StringLength(10)]
        [DisplayName("Phone")]
        public virtual string Phone { get; set; }

        [StringLength(5)]
        [DisplayName("Ext")]
        public virtual string Ext { get; set; }

        public bool HasValue
        {
            get
            {
                return (Phone != null || Ext != null);
            }
        }

    }

      [ComplexType]
      public class ContactDetail
      {
          //Constructor
          public ContactDetail()
          {
              phoneInfo = new PhoneInfo();
          }

          [StringLength(100)]
          [DisplayName("Contact Name")]
          [DisplayFormat(NullDisplayText = "")]
          public virtual string Contact { get; set; }

          [Email]
          [StringLength(100)]
          [DisplayName("Email")]
          public virtual string Email { get; set; }

          public virtual PhoneInfo phoneInfo { get; set; }
          public bool HasValue
          {
              get
              {
                  return (Contact != null || Email != null || phoneInfo.HasValue);
              }
          }
      }

/// <summary>
/// Client class (Client No, Client Name, Address, Contact1, Contact2 info, Created By, Modified By (operator and date)
/// </summary>
    public class Client
    {
        public Client()
        {
            Contact1 = new ContactDetail();
            Contact2 = new ContactDetail();
     }

        [Key]
        [Column("ClientId",TypeName = "int")]
        public virtual int Id { get; set; }
        [Required]
        [DisplayName("Client No")]
        [Column("client_no", TypeName = "smallint")]
        public virtual Int16 Number { get; set; }

        [Required]
        [Column("client_name", TypeName = "varchar")]
        [DisplayName("Client Name")]
        [MaxLength(30, ErrorMessage = "Client Name should not be longer than 30 characters" )]
        [MinLength(3, ErrorMessage = "Client Name is too short")]
        public virtual string Name { get; set; }

        [DataType(DataType.MultilineText)]
        public virtual string Address { get; set; }

        public virtual ContactDetail Contact1 {get; set;}
        public virtual ContactDetail Contact2 {get; set;}

        [ForeignKey("EnteredByOperator")]
        public string EnteredBy { get; set; }

        [InverseProperty("ClientsEnteredBy")]
        public virtual Operator EnteredByOperator { get; set; }

        [ForeignKey("ModifiedByOperator")]
        public string ModifiedBy { get; set; }

        [InverseProperty("ClientsUpdatedBy")]
        public virtual Operator ModifiedByOperator { get; set; }

        [DataType(DataType.DateTime)]
        [DisplayName("Created on")]
        public DateTime EnteredOn { get; set; }

        [DataType(DataType.DateTime)]
        [DisplayName("Modified on")]
        public DateTime? ModifiedOn { get; set; }

        public virtual ICollection<ClientOrder> ClientOrders { get; set; }

        public virtual ICollection<Reorder> Reorders { get; set; }
    }
}

我用流利的API映射列名,我也重新定义我原来的库类非常相似,在本教程中<定义A HREF =HTTP://$c$c.msdn.microsoft.com / ASPNET-MVC-应用程序b01a9fe8相对=nofollow> HTTP://$c$c.msdn.microsoft.com/ASPNET-MVC-Application-b01a9fe8

这是一个名为_ClientForm客户端形式我目前的局部视图:

This is my current partial view for the client form called _ClientForm:

@using WebDemo.Helper
@model CardNumbers.Objects.Client
<fieldset>
    <legend>Client Info</legend>

    @Html.ValidationSummary(true)

    <input type="hidden" id="fntype" name="fntype">
    @Html.HiddenFor(model => model.Id)
    @Html.EditorFor(model => model.Number, EditorTemplate.TextBox)

    @Html.EditorFor(model => model.Name, EditorTemplate.TextBox)

    @Html.EditorFor(model => model.Address, EditorTemplate.EditBox)

    <div id="ContactsInfo">
        @*Contact 1*@

        <div id="Contact1">

         @*@Html.EditorFor(model=>model.Contact1)*@
            @Html.EditorFor(model=>model.Contact1.Contact, EditorTemplate.TextBox)
            @Html.EditorFor(model=>model.Contact1.Email, EditorTemplate.TextBox)
        </div>

        @*Contact2*@
        <div id="Contact2">

        @*   @Html.EditorFor(model => model.Contact2)*@
        </div>
    </div>
    @*<div class="clear"></div>*@
    <div id="SaveCancel" class="float-right">
        <button type="Submit" id="btnSave">Save</button>
        <button type="reset" id="btnCancel">Cancel</button>
    </div>
</fieldset>

我已经尝试恢复到原始的方式只有一个级别的,我也谈到第二Contact2信息,但仍然是电子邮件验证不起作用,和所有其他的验证也似乎没有工作。

I already tried to revert to original way of only one level and I also commented the second Contact2 info but still the e-mail validation doesn't work and all other validations also don't seem to work.

该EditorFor文本框定义基于这个博客帖子<一个href=\"http://fusionovation.com/post/2010/02/15/adding-a-rich-text-editor-to-asp-net-mvc-using-strongly-typed-helpers-dataannotations-amp-jquery.aspx\" rel=\"nofollow\">http://fusionovation.com/post/2010/02/15/adding-a-rich-text-editor-to-asp-net-mvc-using-strongly-typed-helpers-dataannotations-amp-jquery.aspx

The EditorFor textboxes are defined based on this blog post http://fusionovation.com/post/2010/02/15/adding-a-rich-text-editor-to-asp-net-mvc-using-strongly-typed-helpers-dataannotations-amp-jquery.aspx

这是两件新EditorFor我说的:
PhoneInfo.cshtml

And these are two of the new EditorFor I added: PhoneInfo.cshtml

@using WebDemo.Helper
@model CardNumbers.Objects.PhoneInfo

<div id="PhoneInfo">
    <div class="float-left">
        @Html.EditorFor(model => model.Phone, EditorTemplate.TextBox)
    </div>
    <div class="float-right">
        @Html.EditorFor(model => model.Ext, EditorTemplate.TextBox)
    </div>
</div>

和ContactDetail.cshtml

And ContactDetail.cshtml

@using WebDemo.Helper
@model CardNumbers.Objects.ContactDetail

@Html.EditorFor(model => model.Contact, EditorTemplate.TextBox)
@Html.EditorFor(model => model.Email, EditorTemplate.TextBox)
@Html.EditorFor(model=>model.phoneInfo)

所以,你可以看到,意见的code是现在非常紧凑。

So, as you can see, the code of the views is now very compact.

然而,所有这些到位的验证看不到了火。我曾经通过键入一些垃圾来测试电子邮件验证。它用于提供文本框附近的验证消息。现在我观察到的电子邮件文本框取红色边框,但没有消息。

However, with all of these in place the validations don't see to fire anymore. I used to test validation on EMail by typing some garbage. It used to provide a validation message near the textbox. Now I observe that the email textbox takes the red border, but there is no message.

你明白我现在缺少的,如果它可以使用复杂类型和验证?

Do you see what I am missing now and if it's possible to use complex type and validations?

要澄清,该_ClientForm从该客户端查看名为:

To clarify, the _ClientForm is called from this Client view:

@model CardNumbers.Objects.Client

@{
    ViewBag.Title = "Client";
}

@section scripts {
    <script src="@Url.Content("~/Scripts/Clients.js")" type="text/javascript" ></script>
}

 <form id="frmClientsSearch">
        <label for="clientNo">Client No: </label>
        <input type="number" name="searchClientNo" class="numericOnly" /><br />
        <label for="clientName">Client Name: </label>
        <input type =  "text" size =25 value ="Please enter the search value" class="SelectOnEntry"
            name ="searchClientName" />

       <input type="button" id="btnClientsSearch" value ="Find / Refresh" />      
</form>
<div style="padding-left: 150px; padding-top: 50px; padding-bottom: 50px;" id="ClientsResults">
    <table id="flexClients" style="display: none">
    </table>
</div>
<div style="display: none">
   <form id="sform" title="Client Info">


        @{Html.RenderPartial("_ClientForm", Model)   ;}

    </form>
</div>

感谢。

推荐答案

一些试验和错误之后,我发现文本框EditorFor认为是罪魁祸首。我记录我在我的答案在这里找到的http://forums.asp.net/t/1855963.aspx/1?Validation+messages+don+t+show+up+what+is+missing+

After some trials and error I found that the TextBox EditorFor view was the culprit. I documented what I found in my answer here http://forums.asp.net/t/1855963.aspx/1?Validation+messages+don+t+show+up+what+is+missing+

基本上,只要我用这个EditorFor

Basically, as long as I use this EditorFor

@*@using WebDemo.Helper*@
@model CardNumbers.Objects.PhoneInfo

<div id="PhoneInfo">
    <div class="float-left">
        @* @Html.EditorFor(model => model.Phone, EditorTemplate.TextBox)*@
        <div class="editor-label">
            @Html.LabelFor(model => model.Phone)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Phone)
            @Html.ValidationMessageFor(model => model.Phone)
        </div>
    </div>
    <div class="float-right">
        @*@Html.EditorFor(model => model.Ext, EditorTemplate.TextBox)*@
        <div class="editor-label">
            @Html.LabelFor(model => model.Ext)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Ext)
            @Html.ValidationMessageFor(model => model.Ext)
        </div>
    </div>
</div>

一切似乎工作确定。但是,如果我尝试切换到一个较短的语法和使用EditorFor的文本框:

All seems to work OK. But if I try to switch to a shorter syntax and use this EditorFor for the textbox:

<div class="editor-label">
    @Html.Label((ViewData.ModelMetadata.DisplayName??ViewData.ModelMetadata.PropertyName),
        new Dictionary<string, object>
            {
                { "for", ViewData.ModelMetadata.PropertyName }
            })
</div>
<div class="editor-field">
    @Html.TextBox("", (object)Model,
        new Dictionary<string, object>
            {
                { "id", ViewData.ModelMetadata.PropertyName },
                { "name", ViewData.ModelMetadata.PropertyName },
                { "class", "text-box single-line"},
                { "data-bind", "value: " + ViewData.ModelMetadata.PropertyName },
            })
    @Html.ValidationMessage(ViewData.ModelMetadata.PropertyName,
        new Dictionary<string, object>
            {
                { "data-valmsg-for", ViewData.ModelMetadata.PropertyName }
            })
</div>

确认消息不会再显示出来。

Validation messages do not show anymore.

希望这个答案将帮助别人或者您可能会看到什么,我在这里失踪。

Hopefully this answer will help someone or you may see what I am missing here.

这篇关于验证显示不出来使用EF code先用复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆