这是这个问题模型类和映射的延续
我的 Client 类现在工作正常,它被定义为
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中定义的非常相似
这是我当前对名为 _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 信息,但电子邮件验证仍然不起作用,所有其他验证似乎也不起作用。
EditorFor 文本框是根据这篇博文定义的http://fusionovation.com/post/2010/02/15/adding-a-rich-text-editor-to-asp-net-mvc-using-strongly-typed-助手-数据注释-amp-jquery.aspx
这些是我添加的两个新 EditorFor: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
@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)
因此,如您所见,视图的代码现在非常紧凑。
然而,随着所有这些都到位,验证不会再触发了。我曾经通过输入一些垃圾来测试电子邮件验证。它用于在文本框附近提供验证消息。现在我观察到电子邮件文本框采用红色边框,但没有消息。
您是否看到我现在缺少的内容以及是否可以使用复杂的类型和验证?
为了澄清,_ClientForm 是从这个客户端视图中调用的:
@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>
谢谢。