验证不会出现使用EF Code First与复杂类型 [英] Validations not show up using EF Code First with complex types
问题描述
这是这个问题的延续模型类和映射
我的客户端类现在正常工作,它被定义为
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; }
}
}
我使用Fluent API映射了列名,我也重新定义我的原始存储库类与本教程中定义的非常相似。 http://code.msdn.microsoft.com/ASPNET-MVC-Application-b01a9fe8
I mapped column names using Fluent API and I also re-defined my original "repository" classes to be very similar to defined in this tutorial http://code.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.
编辑器对于文本框是基于此博客文章定义的 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)
所以,你可以看到,视图的代码现在非常紧凑。
So, as you can see, the code of the views is now very compact.
然而,所有这些都到位,验证不再发生了。我曾经通过键入一些垃圾来测试对EMail的验证。它用于在文本框附近提供验证消息。现在我观察到电子邮件文本框采取红色边框,但没有消息。
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>
谢谢。
推荐答案
经过一些试验和错误,我发现TextBox 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>
所有似乎都可以正常工作。但是,如果我尝试切换到较短的语法,并使用此EditorTo作为文本框:
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>
验证信息不再显示。
希望这个答案可以帮助某人或你看到我在这里失踪的东西。
Hopefully this answer will help someone or you may see what I am missing here.
这篇关于验证不会出现使用EF Code First与复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!