简体   繁体   English

MVC 3-视图模型的子对象上的客户端验证

[英]MVC 3 - Client Side Validation on a child object of a view model

I have a view model that looks like this - 我有一个看起来像这样的视图模型-

public class upgradeViewModel
{
    public Cart Cart { get; set; }
    public RegisterModel RegisterModel { get; set; }
} 

The view model contains a property called RegisterModel the RegisterModel model object looks similar to this - 视图模型包含一个名为RegisterModel的属性, RegisterModel模型对象与此类似-

public class RegisterModel
{
    [Required]
    [Display(Name = "Your name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Your email address")]
    public string Email { get; set; }

    ...
}

I use the RegisterModel property on the view model to set up a form in a view that uses upgradeViewModel object as it's model. 我在视图模型上使用RegisterModel属性在使用upgradeViewModel对象作为模型的视图中设置表单。 On submitting the form I don't get any client side validation, although I do get server side validation for the RegisterModel object. 提交表单时,尽管我确实获得了RegisterModel对象的服务器端验证,但我没有任何客户端验证。

I believe I have everything configured correctly for unobtrusive client side validation to work, and it works in other areas of the web site. 我相信我已经正确配置了所有内容,以使客户端验证不受影响,并且可以在网站的其他区域使用。 Do I need to do something extra to get client side validation to work for a child object of the view model? 我需要做一些额外的事情来使客户端验证适用于视图模型的子对象吗?

Here's my view code - 这是我的查看代码-

@model upgradeViewModel

@{
    ViewBag.Title = "Title";
    Layout = "~/Views/Shared/_LayoutNormal.cshtml";
}

<h2>Title</h2>

Some text ....

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
    <div>
        <fieldset>
            <legend>Your Information</legend>

            <div class="editor-label">
                @Html.LabelFor(m => m.RegisterModel.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.RegisterModel.UserName)
                @Html.ValidationMessageFor(m => m.RegisterModel.UserName)
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.RegisterModel.Email)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.RegisterModel.Email)
                @Html.ValidationMessageFor(m => m.RegisterModel.Email)
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.RegisterModel.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.RegisterModel.Password)
                @Html.ValidationMessageFor(m => m.RegisterModel.Password)
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.RegisterModel.ConfirmPassword)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.RegisterModel.ConfirmPassword)
                @Html.ValidationMessageFor(m => m.RegisterModel.ConfirmPassword)
            </div>

            <p>
                <input type="submit" value="Save Details" class="inputbutton" />
            </p>
        </fieldset>
    </div>
}

You should make sure that the following appsettings are in your web.config file. 您应确保web.config文件中包含以下appsettings。 And you have referenced jquery.validate.unobtrusive.min.js on your page. 并且您在页面上引用了jquery.validate.unobtrusive.min.js。 (Which should be in the Scripts folder of your MVC project when you create it) (创建时应位于MVC项目的Scripts文件夹中)

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

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

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