2013-04-01 90 views
1

我有一個MVC3 C#.Net Web App。我們正在使用ckEditor庫來增強我們應用中的TextAreas。使用標準TextArea時,驗證操作正確。但是,在增強的TextAreas(實現ckEditor)中,提交頁面時,驗證會觸發錯誤。即使TextArea中存在數據,「描述是必需的」。第二次點擊提交後,表單提交正常。MVC3 C#TextArea/CkEditor驗證問題

域類屬性:

[Display(Name = "Description")] 
[Required(ErrorMessage = "Description is required.")] 
public virtual string Description { get; set; } 

HTML:

<td style="border: 0;text-align:left " > 
     @Html.TextAreaFor(model => model.Description, 
      new 
      { 
       rows = 5, 
       cols = 100, 
       @class = "celltext2 save-alert" 
      }) 
      @Html.ValidationMessageFor(model => model.Description) 
    </td> 

我認爲應用CKEDITOR屬性以某種方式搞亂起來。想法?`

讓我澄清更多。我們有一個查詢控件的.js文件,然後執行ckEditor初始化。當我刪除$(this).ckeditor({})它工作正常。

JS文件:

$('textarea').each(function() { 

    $(this).ckeditor({}); 

}); 

回答

0

像這樣的東西可能會奏效:

$('textarea').each(function() { 
    var textarea = $(this); 
    $(this).ckeditor({}).on('blur', function() { 
     textarea.html($(this).html()); 
    }); 
}); 

EDIT(我從來沒有使用jQuery的適配器,一個小的教訓,我發現這個工作,上述後模糊從未觸發和$(this).html()沒有定義):

$('textarea').each(function() { 
    var textarea = $(this); 
    textarea.ckeditor(function() { 
     textarea.ckeditorGet().on('blur', function() { 
      textarea.html(this.getData()); 
     }); 
    }); 
}); 
+0

@Dave ...我喜歡這個主意。儘管如此,仍然沒有奏效。我在考慮.ckeditor初始化正在做一些事情,讓textarea感到困惑 – MikeTWebb

+0

更新了我的答案。 – Dave

+0

@戴夫..感謝。我會檢查出來的 – MikeTWebb

0

我覺得這比這個簡單一點。 CKE創建一個使用它的iframe而不是實際的textarea,並且在提交之前,您確實需要使用CKEditor中的數據更新textarea的內容。然而,我會建議你在提交而不是模糊時進行此操作。我會建議設置一個id到相關的DOM元素,但你明白了。

// Replace textarea(s) 
$('textarea').each(function() { 
    $(this).ckeditor({}); 
}); 

// Bind on submit event to update the text 
$('form').submit(function() { 
    // Upate textarea value with the ckeditor data 
    $('textarea').val(CKEDITOR.instances.ValueCKE.getData()); 
});