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文件,但是,我也不想为相同的事件和元素连接一个新的处理程序,因为它已经存在。