本文介绍了子视图上的MVC验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Sitecore/MVC应用程序,这是我的第一个MVC应用程序,因此我正在学习.毫无疑问,我在某处出错了.

I am working on a Sitecore/MVC application, my first MVC application so I am learning as I go. No doubt I am going wrong somewhere along the line.

我有一个购物篮,上面有2个地址视图,一个用于帐单,另一个用于送货.还有一个与帐单相同的交付方式"复选框,允许用户仅填写一个地址.当您选中此复选框时,收信人地址div会折叠.

I have a Basket that has 2 address views on it, one for billing and another for delivery. There is also a checkbox for "Delivery the same as billing" allowing the user to complete just one address. When you user checks this checkbox the delivery address div collapses.

主视图:

<div class="pure-control-group">
         <h2>Billing Address</h2>
        @Html.Action("Init", "Address", new {AddressType = "Billing", @Address = Model.Billing})
    </div>
    <!-- Delivery Address-->
    <div class="pure-control-group">
        <h2>Delivery Address</h2>
        <label for="UseBillingForShipping" class="pure-checkbox">
            @Html.CheckBoxFor(x => x.UseBillingForShipping)

            Same as Billing Address above
        </label>
    </div>
    <div class="manual-address-entry focus-pane">
        @Html.Action("Init", "Address", new {AddressType = "Delivery", @Address = Model.Delivery})
</div>

地址"视图的示例:

<div class="pure-u-1 pure-u-sm-1-2 pure-u-lg-2-5">
    <label for="@(Model.AddressType).FirstName">First Name<span class="required">*</span></label>
    <input type="text" id="@(Model.AddressType).FirstName" name="@(Model.AddressType).FirstName">
    @Html.ValidationMessageFor(x=>x.FirstName) //<= How to handle this?

</div>
<div class="pure-u-1 pure-u-sm-1-2 pure-u-lg-2-5">
    <label for="@(Model.AddressType).LastName">Last Name<span class="required">*</span></label>
    <input type="text" id="@(Model.AddressType).LastName" name="@(Model.AddressType).LastName">
    @Html.ValidationMessageFor(x=>x.LastName) //<= How to handle this?
</div>

当我尝试验证时出现我的问题.地址视图上控件的ID命名为id="@(Model.AddressType).LastName",因此在帐单地址的情况下,它们的呈现方式类似于id="Billing.LastName"

My problem occurs when I am trying to validate. The id of the controls on the address view are named id="@(Model.AddressType).LastName" so in the case of the Billing address they render like id="Billing.LastName"

在地址模型上,对字段进行注释,例如:

On the Address model the fields are annotated, e.g:

[Required(ErrorMessage = "First Name is required")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
public string LastName { get; set; }

所以我有2个问题:

  1. 如何创建@Html.ValidationMessageFor标记.我已经尝试过@Html.ValidationMessageFor(x=>x.FirstName)和类似于labelfor(<label for="@(Model.AddressType).LastName">),@Html.ValidationMessageFor(@(Model.AddressType).LastName)的东西,但是都没有用.我开始认为我完全错误地采用了这种方法.
  2. 第二个是如果用户选中相同地址的复选框,我将如何关闭仅对第二个地址的验证.
  1. How do I create the @Html.ValidationMessageFor markup. I have tried @Html.ValidationMessageFor(x=>x.FirstName) and something similar to the labelfor (<label for="@(Model.AddressType).LastName">), @Html.ValidationMessageFor(@(Model.AddressType).LastName) and neither work. I am starting to think I have approached this totally the wrong way.
  2. The second is if the user selects the checkbox for same address how would I go about switching off validation for the second address only.

推荐答案

处理此问题的最简单方法是对地址模型使用自定义EditorTemplate.假定其为public class Address,然后在名为Address.cshtml/Views/Shared/EditorTemplates中创建一个视图(即,命名为与您的类型名称匹配)

The easiest way to handle this is to use a custom EditorTemplate for your address model. Assuming its public class Address, then create a view in /Views/Shared/EditorTemplates named Address.cshtml (i.e. named to match the name of your type)

@model yourAssembly.Address
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName)
@Html.ValidationMessageFor(m => m.FirstName)
... // ditto for other properties of Address

然后在主视图中

@Html.EditorFor(m => m.Billing)
@Html.CheckBoxFor(x => x.UseBillingForShipping)
@Html.EditorFor(m => m.Delivery)

EditorFor()方法将使用您的模板并正确命名所有要绑定的元素(包括验证消息)

The EditorFor() method will use your template and correctly name all elements for binding (including the validation message)

请注意,因为您具有[Required]属性,所以隐藏交货"地址的脚本还应确保将结算"的内容复制到交货"地址控件中,否则验证将失败(或者您可以使用[RequiredIf]验证属性)

Note that because you have a [Required] attribute, the script that hides the 'Delivery' address, should also ensure that it copies the contents of the 'Billing' to the 'Delivery' address controls otherwise validation will fail (alternatively you could use a [RequiredIf] validation attribute)

这篇关于子视图上的MVC验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:25