简体   繁体   中英

MVC 4 ViewModel unobtrusive validation not displaying

I have a ViewModel that I am using to POST data back to the server.

[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

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.

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

So why, do only some of the validation errors display on the form after an attempted 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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