我正在做一个MVC教程并遇到了问题。
我将使用Html.BeginForm的工作视图更改为Ajax.BeginForm。 但表格没有提交。
根据我的研究,我验证了在web.config中ClientValidationEnabled和UnobtrusiveJavaScriptEnabled都设置为true。 我确实有最新版本的jquery.unobtrusive-ajax,js和jquery refrenced。 但仍然没有快乐。
我发现了jquery.unobtrusive-ajax,看看它在做什么。 它正在设置页面加载。 但是当我单击“提交”按钮时,不会调用任何函数。
以下是View Source的一个片段,展示了如何开始设置表单标记:
<div id="formSection"> <h3>Having trouble? Send me a message</h3> <form action="/Home/Contact" data-ajax="true" data-ajax-method="post" data-ajax-mode="replace" data-ajax-update="#formSection" id="form0" method="post"> </form> <textarea name="message"></textarea> <input type="submit" value="send" /> </div>I am doing an MVC tutorial and have hit a problem.
I changed a working view from using Html.BeginForm to Ajax.BeginForm. But the form is not submitting.
Per my research I verified that ClientValidationEnabled and UnobtrusiveJavaScriptEnabled are both set to true in web.config. I do have the most recent version of jquery.unobtrusive-ajax,js and jquery refrenced. But still no joy.
I firebugged jquery.unobtrusive-ajax to see what it was doing. It is setting itself up on page load. But no functions are called when I click the submit button.
Here is a snippet from View Source to show how the form tag is begin set up:
<div id="formSection"> <h3>Having trouble? Send me a message</h3> <form action="/Home/Contact" data-ajax="true" data-ajax-method="post" data-ajax-mode="replace" data-ajax-update="#formSection" id="form0" method="post"> </form> <textarea name="message"></textarea> <input type="submit" value="send" /> </div>最满意答案
在提问时,我的错误变得明显。 当我看到结束表单标记在提交按钮之前时,我正在清理HTML的错误格式!
我的Razor代码出错了。
@Using (Ajax.BeginForm("Contact", "Home", New AjaxOptions With {.HttpMethod = "post", .InsertionMode = InsertionMode.Replace, .UpdateTargetId = "formSection"})) End Using <textarea name="message"></textarea> <input type="submit" value="send" />在使用之后立即使用结束使用当然关闭了块,因此输入元素永远不会进入表单。
正确的语法:
@Using (Ajax.BeginForm("Contact", "Home", New AjaxOptions With {.HttpMethod = "post", .InsertionMode = InsertionMode.Replace, .UpdateTargetId = "formSection"})) @<textarea name="message"></textarea> @<input type="submit" value="send" /> End Using如果生成的代码格式化得很好,我会更快地看到结束表单标记位置。
希望我帮助避免一些头部打击。
My error became apparent while asking the question. I was cleaning up the bad formatting of the HTML when I saw the end form tag was before the submit button!
I had an error in my Razor code.
@Using (Ajax.BeginForm("Contact", "Home", New AjaxOptions With {.HttpMethod = "post", .InsertionMode = InsertionMode.Replace, .UpdateTargetId = "formSection"})) End Using <textarea name="message"></textarea> <input type="submit" value="send" />Putting the End Using right after the Using closed the block of course, so the input element never got into the form.
Correct syntax:
@Using (Ajax.BeginForm("Contact", "Home", New AjaxOptions With {.HttpMethod = "post", .InsertionMode = InsertionMode.Replace, .UpdateTargetId = "formSection"})) @<textarea name="message"></textarea> @<input type="submit" value="send" /> End UsingIt would have been nice if the generated code was formatted well, I would have seen the end form tag location much sooner.
Hopefully I help avoid a few head slaps.
更多推荐
发布评论