0

我一直在抨擊我的頭靠在牆上,試圖讓這個工作,我終於決定尋求幫助。我必須在我的表單中提交按鈕。一個提交按鈕允許用戶返回。它需要是一個提交按鈕,因爲我仍然需要使用隱藏輸入字段提交的表單。 JQuery.unobtrusive.ajax與「取消」類(MVC 5)不兼容(MVC 5)

<form action="/Question/ProcessQuestion" data-ajax="true" data-ajax-failure="handleError" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#questionForm" data-ajax-url="/Question/ProcessQuestion" id="form0" method="post"> 
    <input type="hidden" name="TraversalId" value="a59aff78-d10c-4800-b91a-3ae47a95a52c"> 
    <input type="hidden" name="AssetId" value="1cf261a6-4549-4802-bd43-f2900178604d"> 
    <input type="hidden" name="Type" value="question/text"> 
    <div id="textQuestion"> 
     <div class="bodyText"> 
      <div>In the space below, describe the problem with the left ankle, using as many details as possible.</div><br> 
      <textarea rows="10" cols="50" maxlength="999" name="TextResponse" class="textarea" required=""></textarea> 
      <div class="bigButtons"><br> 
       <input type="submit" id="btnBack" value="Back" name="buttonType" class="cancel"> 
       <input type="submit" id="btnContinue" value="Continue" name="buttonType"> 
      </div> 
     </div> 
    </div> 
</form> 

正如你所看到的「返回」按鈕,有一個名爲「取消」,在它的類。根據幾個消息來源,即使驗證失敗,也應該提交表單。我遵循stackoverflow的建議。它不起作用。我也看到了asp.net團隊在哪裏表示它已被修復asp.net codeplex,但事實並非如此,因爲它不適合我。

我一直在調試代碼,並且執行永遠不會進入下面的函數。

$(document).on("submit", "form[data-ajax=true]", function (evt) { 
    var clickInfo = $(this).data(data_click) || [], 
     clickTarget = $(this).data(data_target), 
     isCancel = clickTarget && clickTarget.hasClass("cancel"); 
    evt.preventDefault(); 
    if (!isCancel && !validate(this)) { 
     return; 
    } 
    asyncRequest(this, { 
     url: this.action, 
     type: this.method || "GET", 
     data: clickInfo.concat($(this).serializeArray()) 
    }); 
}); 

相反textarea的高亮顯示和快速彈出消息指出「請填寫此場」。我相信我做錯了什麼,但是,我不知道它是什麼。我將不勝感激任何幫助。

回答

0

我終於選擇了更改jquery.unobtrusive.ajax.js代碼。我在下面的事件處理程序中添加了一些邏輯。

$(document).on("click", "form[data-ajax=true] :submit", function (evt) { 
    var name = evt.currentTarget.name, 
     target = $(evt.target), 
     form = $(target.parents("form[data-ajax=true]")[0]); 

    form.data(data_click, name ?[{ name: name, value: evt.currentTarget.value }]: []); 
    form.data(data_target, target); 

    setTimeout(function() { 
     form.removeData(data_click); 
     form.removeData(data_target); 
    }, 0); 
}); 

我在變量聲明之後添加了這個邏輯。

if (target.hasClass("cancel")) { 
    var clickInfo = name ? [{ name: name, value: evt.currentTarget.value }] : []; 
    var options = { 
     url : form.attr("action"), 
     type: form.attr("method"), 
     data: clickInfo.concat(form.serializeArray()) 
    }; 
    $.ajax(options).done(function(result) { 
     var target = $(form.attr("data-ajax-update")); 
     target.replaceWith(result); 
    }).fail(function(result) { 
     if (result.statusText && result.responseText) { 
      handleError(result); 
     } 
    }); 
    return false; 
} 

它現在正在爲我工​​作。如果有人有更好的方法來解決這個問題,我會很感激這些信息。我真的不想修改jquery.unobtrusive.ajax.js文件,但是,我也不想爲相同的事件和元素連接一個新的處理程序,因爲它已經存在。