gpt4 book ai didi

asp.net-mvc - 在ASP.NET MVC表单上禁用非侵入式验证

转载 作者:行者123 更新时间:2023-12-04 19:08:39 24 4
gpt4 key购买 nike

我在页面上有两种形式。我希望第一种形式使用非侵入式验证,因为它是由ASP.NET MVC框架自动生成的。但是,还有第二种形式(我手动编写),不应使用无干扰的验证。

这是一些代码:

@using (Html.BeginForm("Add", "Contacts", FormMethod.Post, new { id = "AddForm" }))
{
@Html.ValidationSummary(true)

<fieldset>
<legend>Datos Contacto</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ContactDepartmentID)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ContactDepartmentID, (List<SelectListItem>)ViewBag.ContactDepartments)
@Html.ValidationMessageFor(model => model.ContactDepartmentID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Sex)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Sex, (List<SelectListItem>)ViewBag.Sexs)
@Html.ValidationMessageFor(model => model.Sex)
</div>
</fieldset>
<br />
@Html.HiddenFor(model => model.SerializedEmails, new { data_bind = "value: ko.toJSON($root.emails())" })
@Html.HiddenFor(model => model.SerializedPhones, new { data_bind = "value: ko.toJSON($root.phones())" })
}
<form id="phoneForm" >
<fieldset>
<legend>Teléfonos</legend>
<table class="table">
<tbody data-bind="foreach: phones">
<tr>
<th>
Celular?
</th>
<th>
Phone
</th>
<th>
Extension
</th>
<th>
</th>
</tr>
<tr>
<td>
<input type="checkbox" data-bind="checked: isMobile" />
</td>
<td>
<input class="phone" data-bind='value: phone'/>
</td>
<td>
<input type="text" class="extension" data-bind='value: phoneExtension, enable: !isMobile() ' />
</td>
<td>
<a href='#' data-bind='click: $root.removePhone'>Delete</a>
</td>
</tr>
</tbody>
</table>
<a href='#' data-bind='click: $root.addPhone'>Agregar teléfono</a>
</fieldset>
</form>
<p>
<button onclick="Submit();" type="button" class="btn btn-primary" data-bind='enable: phones().length > 0 || emails().length > 0'>
Create</button>
</p>

JS:

function Submit()
{
var valid = $('#AddForm').valid();
var valid2 = $('#phoneForm').valid();
}

jQuery.validator.addClassRules("phone", {
required: true
});

附带说明:当我从页面中删除非干扰性验证时,第二种形式进行验证,而第一种形式则无效。如果我使用非侵入式验证,则第一种形式会验证,但第二种形式不会。

我知道我可以在客户端进行整个验证-并且,如果那是唯一的方法,我会做。我正在考虑一种继续使用非侵入式验证的方法,但允许使用例如自定义属性。

最佳答案

您可以通过以下Html Helper属性从 Razor 代码中禁用不打扰的验证:

HtmlHelper.ClientValidationEnabled = false;

这样,您可以根据其特定 View /局部 View 中的此设置对不同的表单进行不打扰的验证。

关于asp.net-mvc - 在ASP.NET MVC表单上禁用非侵入式验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18151735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com