ASP.NET MVC 3 Ajax.BeginForm()在Views / Shared Folder中找不到Controller Action?

时间:2012-10-19 23:13:48

标签: ajax asp.net-mvc-3

任何人都知道这里的交易是什么?如果我将Ajax.BeginForm放入

中的cshtml文件中

观看/共享文件夹

它不起作用:

 @using (Ajax.BeginForm("TestAction", "Test", new AjaxOptions
 {
     HttpMethod = "Post"
 }
 , new { id = "submitTestForm" })) {}

但是常规的Html.BeginForm会:

@using (Html.BeginForm("TestAction","Test",FormMethod.Post,new {id="submitTestForm"})) {}

但如果我在

下移动它

Views / Test文件夹(本例中的控制器是TestController)

工作正常。

这是一个错误吗?或者我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

不确定为什么它不能在共享文件夹中工作,但你可以摆脱ajax表单并使用普通表单并自己写一些手写的CLEAN javascript来做同样的事情

@using(Html.Beginform("testAction","test",FormMethod.Post, new { id="submitTestFrm"})
{

  <input type="submit" />
}

<script type="text/javascript">
 $(function(){
     $("#submitTestFrm").submit(function(e){
        e.preventDefault();
        var _this=$(this);
        $.post(_this.attr("action"),_this.serialize(),function(response){
          //do something with the response.
      });
     });    
 });
</script>

答案 1 :(得分:0)

当我意识到我实际上正在使用BeginForm方法的错误重载时,我遇到了同样的问题(但在ASP.Net MVC 5 中)。简单地改为这就解决了我的问题:

@using (Html.BeginForm("TestAction","Test", null, FormMethod.Post, new {id="submitTestForm"})) {}

注意为routevalues对象发送 null 参数。