Validations not show up using EF Code First with complex types

前端 未结 2 1094
梦谈多话
梦谈多话 2021-01-29 05:08

This is a continuation of this question Model class and Mapping

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

using System;

using System.         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-29 05:31

    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+

    Basically, as long as I use this EditorFor

    @*@using WebDemo.Helper*@
    @model CardNumbers.Objects.PhoneInfo
    
    
    @* @Html.EditorFor(model => model.Phone, EditorTemplate.TextBox)*@
    @Html.LabelFor(model => model.Phone)
    @Html.EditorFor(model => model.Phone) @Html.ValidationMessageFor(model => model.Phone)
    @*@Html.EditorFor(model => model.Ext, EditorTemplate.TextBox)*@
    @Html.LabelFor(model => model.Ext)
    @Html.EditorFor(model => model.Ext) @Html.ValidationMessageFor(model => model.Ext)

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

    @Html.Label((ViewData.ModelMetadata.DisplayName??ViewData.ModelMetadata.PropertyName), new Dictionary { { "for", ViewData.ModelMetadata.PropertyName } })
    @Html.TextBox("", (object)Model, new Dictionary { { "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 { { "data-valmsg-for", ViewData.ModelMetadata.PropertyName } })

    Validation messages do not show anymore.

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

提交回复
热议问题