[英]MVC4: jQuery Validation Unobtrusive Native working incorrectly
我的MVC4網絡應用程序是在Umbraco 7上構建的。我已經安裝了以下nuget包:
jQuery 1.10.2
jQuery.Validation 1.11.1
jQuery.Validation.Unobtrusive.Native.MVC4 1.1.0.0
不引人注目的驗證來自這個網站: http : //jqueryvalidationunobtrusivenative.azurewebsites.net/Home/GettingStarted
我的觀點如下:
<form id="frmContact" method="post" action="@Url.Action("ContactForm", "ContactFormSurface")">
<div>
<label>Full Name</label>
@Html.TextBoxFor(m => m.FullName, true)
@Html.ValidationMessageFor(m => m.FullName, "*")
</div>
<div>
<label>Company</label>
@Html.TextBoxFor(m => m.Company, true)
@Html.ValidationMessageFor(m => m.Company, "*")
</div>
<div>
<label>Email Address</label>
@Html.TextBoxFor(m => m.EmailAddress, true)
@Html.ValidationMessageFor(m => m.EmailAddress, "*")
</div>
<div>
<label>Message @Html.ValidationMessageFor(m => m.Message, "*")</label>
@Html.TextAreaFor(m => m.Message, true, 10, 30, new { @class = "input-xl" })
</div>
@Html.ValidationSummary(false, "Please correct the following errors before submitting the message")
<div>
<button type="submit">Send</button>
</div>
</form>
在web.config中:
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
我的模特:
public class ContactFormViewModel
{
[Required, Display(Name = "Full Name")]
public string FullName { get; set; }
public string Company { get; set; }
[Required, Display(Name = "Email Address")]
[RegularExpression(@"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$", ErrorMessage="Not a valid email address")]
public string EmailAddress { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string Message { get; set; }
}
這是腳本(我已經確認在瀏覽器中正確加載)
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
呈現的HTML看起來像這樣(在初始頁面加載時):
<form action="/umbraco/Surface/ContactFormSurface/ContactForm" method="post" id="frmContact">
<div>
<label>Full Name</label>
<input type="text" value="" name="FullName" id="FullName" data-rule-required="true" data-msg-required="The Full Name field is required." class="input-validation-error">
<span data-valmsg-replace="false" data-valmsg-for="FullName" class="field-validation-error">*</span>
</div>
<div>
<label>Company</label>
<input type="text" value="" name="Company" id="Company">
<span data-valmsg-replace="false" data-valmsg-for="Company" class="field-validation-valid">*</span>
</div>
<div>
<label>Email Address</label>
<input type="text" value="" name="EmailAddress" id="EmailAddress" data-rule-required="true" data-rule-regex="{"pattern":"^(?!\\.)(\"([^\"\\r\\\\]|\\\\[\"\\r\\\\])*\"|([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\\.)\\.)*)(?<!\\.)@[a-z0-9][\\w\\.-]*[a-z0-9]\\.[a-z][a-z\\.]*[a-z]$"}" data-msg-required="The Email Address field is required." data-msg-regex="Not a valid email address" class="input-validation-error">
<span data-valmsg-replace="false" data-valmsg-for="EmailAddress" class="field-validation-error">*</span>
</div>
<div>
<label>Message <span data-valmsg-replace="false" data-valmsg-for="Message" class="field-validation-error">*</span></label>
<textarea rows="10" name="Message" id="Message" data-rule-required="true" data-msg-required="The Message field is required." cols="30" class="input-validation-error input-xl"></textarea>
</div>
<div data-valmsg-summary="true" class="validation-summary-errors"><span>Please correct the following errors before submitting the message</span>
<ul>
<li>The Full Name field is required.</li>
<li>The Email Address field is required.</li>
<li>The Message field is required.</li>
</ul>
</div>
<div>
<button type="submit">Send</button>
</div>
</form>
正如您可能看到的那樣,驗證文本在加載時已經處於無效狀態(在任何按鍵或表單提交之前)。 此外,當我提交空表單時,它會提交回服務器,因此客戶端驗證實際上不會發生。 jQuery驗證腳本運行,因為公司字段不是必需的,並且在驗證消息的類屬性中反映出來( 'field-validation-valid'
)
編輯 :請求的控制器代碼:
public class ContactFormSurfaceController: Umbraco.Web.Mvc.SurfaceController
{
[HttpGet]
public ActionResult ContactForm(ContactFormViewModel model)
{
return PartialView("ContactForm", new ContactFormViewModel());
}
}
有人可以幫幫我嗎? 提前致謝。
不要使用validate(),而是使用valid()
$("#frmId").valid()
請記住在您的頁面中包含validate.unobtrusive.js
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.