简体   繁体   English

MVC 4 ViewModel非侵入式验证未显示

[英]MVC 4 ViewModel unobtrusive validation not displaying

I have a ViewModel that I am using to POST data back to the server. 我有一个ViewModel,用于将数据回传到服务器。

[MetadataType(typeof(CompanyAdminViewModel))]
public class CompanyAdminViewModel
{
    public Company Company { get; set; }
    public RegisterModel User { get; set; }

    public CompanyAdminViewModel()
    {

    }
}

The Company Entity has child entities: Company.CompanyContacts 公司实体有子实体:Company.CompanyContacts

public class CompanyContact
{
    public int CompanyContactId { get; set; }
    public int JobTitleId { get; set; }
    public int CompanyId { get; set; }
    public string Title { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    public Nullable<DateTime> BirthDate { get; set; }
    public string Gender { get; set; }
    [Required]
    public string Phone { get; set; }
    public string Fax { get; set; }
    public string Extension { get; set; }
    [Required]
    public string Email { get; set; }
    public Nullable<DateTime> HireDate { get; set; }
    public virtual Company Company { get; set; }

    public virtual JobTitle JobTitle { get; set; }

    public bool IsActive { get; set; }
}

When I view the pagesource , the data-* attributes are correctly rendered for the model properties. 当我查看pagesource时,为模型属性正确呈现了data- *属性。

<div class="editor-label">
                <label for="FirstName">FirstName</label>
            </div>
            <div class="editor-field">
                <input class="text-box single-line" data-val="true" data-val-required="The FirstName field is required." id="FirstName" name="FirstName" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="FirstName" data-valmsg-replace="true"></span>
            </div>

            <div class="editor-label">
                <label for="LastName">LastName</label>
            </div>
            <div class="editor-field">
                <input class="text-box single-line" data-val="true" data-val-required="The LastName field is required." id="LastName" name="LastName" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="LastName" data-valmsg-replace="true"></span>
            </div>

When I POST the form, only the password property displays the validation error. 当我发布表单时,只有密码属性显示验证错误。 When I check the Model.IsValid, all the failed validations are in the collection... 当我检查Model.IsValid时,所有失败的验证都在集合中...

So why, do only some of the validation errors display on the form after an attempted POST? 那么,为什么在尝试POST之后仅在表单中显示一些验证错误?

It would be useful if you added everything about the form ;) 如果您添加了有关表单的所有内容,这将很有用;)

My question though, is does it submit? 我的问题是,它提交了吗? You have the required filter on a number of fields but with no message (so it probably displays no errors, but does not submit). 您在许多字段上都具有必需的过滤器,但是没有消息(因此它可能不显示任何错误,但不会提交)。

Have you tried these: 您是否尝试过这些:

@Html.ValidationSummary()

And (for the fields you want validated: 并且(对于您要验证的字段:

@Html.ValidationMessageFor(m => m.FirstName)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM