0

这是这个问题模型类和映射的延续

我的 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>

谢谢。

4

2 回答 2

1

我在您的页面上的任何地方都没有看到表单。验证工作需要表单上下文。您需要将 Editor 属性包装在 BeginForm 块中。

于 2012-10-22T17:44:57.977 回答
0

经过一些试验和错误,我发现 TextBox EditorFor 视图是罪魁祸首。我在这里记录了我在答案中找到的内容http://forums.asp.net/t/1855963.aspx/1?Validation+messages+don​​+t+show+up+ what+is+missing+

基本上只要我用这个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 用于文本框:

<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>

验证消息不再显示。

希望这个答案会对某人有所帮助,或者您可能会在这里看到我缺少的东西。

于 2012-11-05T17:29:06.463 回答