2014-09-05 62 views
1

我有我的看法如下HTML:MVC驗證 - [必需]屬性拋出的jQuery JSON.parse異常

@model PatientLookupModel 
@if (Model.IsModelInvalid) 
{ 
    @Html.ValidationSummary(false, Model.Resources.InvalidFormMessage) 
} 

@using (Html.BeginForm("Lookup", "Patient", FormMethod.Post, new { role = "form" })) 
{ 
    @Html.Label(Model.Resources.PatientFirstNameLabel) 
    @Html.TextBoxFor(m => m.PatientFirstName, new { @class = "form-control" }) 
} 

我的模型看起來是這樣的:

using RESOURCES = AppResources.Resources;  
namespace Models 
{ 
    public class PatientLookupModel 
    { 
     [Required(ErrorMessageResourceType = typeof(RESOURCES), ErrorMessageResourceName = "Patient_Lookup_PatientFirstNameRequiredMessage")] 
     public string PatientFirstName { get; set; } 
    } 
} 

我BundleConfig看起來是這樣的:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
        "~/Scripts/lib/jquery-{version}.js", 
        "~/scripts/lib/jquery.unobtrusive*", 
        "~/Scripts/lib/jquery.validate*", 
        "~/Scripts/lib/jquery.maskedinput.js")); 

表單本身按預期工作。當沒有輸入名字時,頁面會重新加載填充的驗證摘要。問題是,當你鍵入的第一個名字輸入的東西,然後它失去焦點,一個例外是在瀏覽器控制檯拋出:

enter image description here

data是不確定的。

任何想法?

+2

http://stackoverflow.com/a/14822755/1893261看看這篇文章,讓我知道它是否有幫助。 – 2014-09-08 13:02:21

+0

@ShukhratRaimov:我以前曾嘗試加載jquery的早期版本(我使用1.11.1),並注意到它有不同的行爲。使用1.11.1,它實際上會回發,但版本<1.9只是將錯誤類應用到元素並將它們放在焦點上。我不確定這種行爲是否可以接受。 – im1dermike 2014-09-08 13:56:18

回答

1

刪除對jquery.unobtrusive-ajax.jsjquery.validate*的引用實際上是我想要做的。出於某種原因(也許是由@ShukhratRaimov確定的原因),當我使用jQuery 1.11.1時,客戶端驗證會中斷。我實際上甚至不想要客戶端驗證 - 我希望應用回發並顯示模型錯誤。

+0

是的,Jquery.validate與微軟的不顯眼的驗證衝突。接得好。 – 2014-09-08 17:40:38