我有一个以下代码
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "update_panel",Url = "/Part/SearchPart" })) { <input type="text" placeholder="Search Parts"/> <input type="submit" value="Search"/> }
它输出以下HTML
<form action="/Part" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#update_panel" data-ajax-url="/Part/SearchPart" id="form0" method="post"> <input type="text" placeholder="Search Parts"/> <input type="submit" value="Search"/> </form>
解决方法
您可以使用
one of the overloads,允许您指定html属性::
@using (Ajax.BeginForm(null,null,new AjaxOptions { UpdateTargetId = "update_panel",Url = "/Part/SearchPart" },new { @class = "pull-right" })) { <input type="text" placeholder="Search Parts"/> <input type="submit" value="Search"/> }
此外,我强烈建议您依靠窗体的url,而不是在AjaxOptions中进行硬编码,因为在虚拟目录中部署应用程序时,您的硬编码/ Part / SearchPart网址将无法正常工作.更不用说在Global.asax中更改路由模式.所以:
@using (Ajax.BeginForm("SearchPart","Part",new AjaxOptions { UpdateTargetId = "update_panel" },new { @class = "pull-right" })) { <input type="text" placeholder="Search Parts"/> <input type="submit" value="Search"/> }