我在我的視圖中使用ajax.beginform。在Html.BeginForm中的httppost之前mvc 4剃刀自定義客戶端驗證
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
{
<table>
<tr>
<td>
<input type="text" id="txtCardNumber">
</td>
<td>
<button id="Search"/>
</td>
</tr>
</table>
}
搜索按鈕點擊直接進入控制器上HTTPPost我想先做像支票卡號一些客戶端驗證,如果有數字和其他自定義的驗證來代替。如果任何驗證失敗,我不想去服務器端。 所以我試圖做返回假,但它掛了很長時間,而不是瞬間。我試過event.preventdefault,但我無法撤消它,並通過適當的值傳遞模型值到服務器端。
$("#Search").button({.live("click", function (event) {
var isvalid = SubmitValidations();
if (isvalid) {
//show progress bar
return true;
} else {
event.preventDefault();
alert("Please correct the errors on the screen before proceeding.");
return false;
}
});
如何執行自定義客戶端驗證並僅在客戶端驗證成功時才轉到服務器端。
如果調試腳本 「的isValid」 永遠返回true? event.preventDefault()應該爲你工作 –
是的,它返回true時,我的自定義驗證通過。但是,event.preventDefault()在正確的情況下將其正常的HTTPPost操作弄亂了。 – Kamayani
當你說什麼搞砸你是什麼意思?在你的問題中,你說你無法使用適當的值進入服務器,它是否會返回,但沒有返回?在這種情況下,我認爲你不需要返回線路。刪除它們後會有什麼變化嗎? –