Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 提交表单后无法访问操作_C#_Asp.net Mvc 3 - Fatal编程技术网

C# 提交表单后无法访问操作

C# 提交表单后无法访问操作,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我有以下问题(我花了很多时间寻找解决方案) “创建”按钮有一个单击事件,该事件调用“主”控制器上的“测试”操作 一切正常 当我点击“保存”按钮提交表单时,这对我来说很好 但在我提交表单后,我的“创建”按钮停止工作。“创建”按钮确实有单击事件,但“测试”操作无法访问 index.cshtml <script type="text/javascript"> $(document).ready(function () { $("#create").click(f

我有以下问题(我花了很多时间寻找解决方案)

“创建”按钮有一个单击事件,该事件调用“主”控制器上的“测试”操作

一切正常

当我点击“保存”按钮提交表单时,这对我来说很好

但在我提交表单后,我的“创建”按钮停止工作。“创建”按钮确实有单击事件,但“测试”操作无法访问

index.cshtml

<script type="text/javascript">
     $(document).ready(function () {
         $("#create").click(function () {
             $.ajax({
                 type: "POST",
                 traditional: true,
                 url: 'Home/Test',
                 dataType: "html",
                 success: function (data) {
                    alert('Succes!')
                 },
                 error: function () {
                     alert('A problem ocurred!!');
                 }
             });

         });
     });
</script>

 <input id="create" type="button" value="Create" />
  @using (Html.BeginForm("SaveForm", "Home"))
  {
      <input type="submit" value="Save" />
  }

您的所有操作都是“仅获取”。将
[HttpPost]
(仅POST)或
[AcceptVerbs(HttpVerbs.POST,HttpVerbs.Get)]
(Get或POST)属性添加到POST操作中

[HttpPost]
public ActionResult Test() 
{
    return Content("Test result");
}

[HttpPost]
public ActionResult SaveForm()
{
    return View("Index");
}

您的所有操作都是“仅获取”。将
[HttpPost]
(仅POST)或
[AcceptVerbs(HttpVerbs.POST,HttpVerbs.Get)]
(Get或POST)属性添加到POST操作中

[HttpPost]
public ActionResult Test() 
{
    return Content("Test result");
}

[HttpPost]
public ActionResult SaveForm()
{
    return View("Index");
}
有两个问题:

1) 您没有[HttpPost]以上的方法

2) 您没有向控制器发送任何数据

使用匿名类将id添加到表单中:

@using (Html.BeginForm("SaveForm", "Home", new {id = "testform"}))
然后重写ajax请求:

<script type="text/javascript">
     $(document).ready(function () {
         $("#create").click(function () {
             $.ajax({
                 type: "POST",
                 data: $("#testform").serialize();
                 url: 'Home/Test',
                 dataType: "html",
                 success: function (data) {
                    alert('Succes!')
                 },
                 error: function () {
                     alert('A problem ocurred!!');
                 }
             });

         });
     });
</script>

$(文档).ready(函数(){
$(“#创建”)。单击(函数(){
$.ajax({
类型:“POST”,
数据:$(“#testform”).serialize();
url:“主页/测试”,
数据类型:“html”,
成功:功能(数据){
警报(“成功!”)
},
错误:函数(){
警报(“出现问题!!”);
}
});
});
});
让我知道它是否有效:)

有两个问题:

1) 您没有[HttpPost]以上的方法

2) 您没有向控制器发送任何数据

使用匿名类将id添加到表单中:

@using (Html.BeginForm("SaveForm", "Home", new {id = "testform"}))
然后重写ajax请求:

<script type="text/javascript">
     $(document).ready(function () {
         $("#create").click(function () {
             $.ajax({
                 type: "POST",
                 data: $("#testform").serialize();
                 url: 'Home/Test',
                 dataType: "html",
                 success: function (data) {
                    alert('Succes!')
                 },
                 error: function () {
                     alert('A problem ocurred!!');
                 }
             });

         });
     });
</script>

$(文档).ready(函数(){
$(“#创建”)。单击(函数(){
$.ajax({
类型:“POST”,
数据:$(“#testform”).serialize();
url:“主页/测试”,
数据类型:“html”,
成功:功能(数据){
警报(“成功!”)
},
错误:函数(){
警报(“出现问题!!”);
}
});
});
});

让我知道它是否有效:)

要创建一个实体,您必须将数据提交到服务器,或者像您的情况一样通过回发或ajax。现在代码中存在一些矛盾:)


我希望这解决了您的问题。

要创建实体,您必须通过回发或ajax将数据提交到服务器。现在代码中存在一些矛盾:)


我希望这解决了您的问题。

最终我使用了Ajax.BeginForm而不是Html.BeginForm,在我的操作中添加了[HttpPost]属性,并使用了RedirectToAction(“索引”)而不是PartialView。这解决了我的问题。再次感谢你的提示

最终,我使用了Ajax.BeginForm而不是Html.BeginForm,在我的操作中添加了[HttpPost]属性,并使用了RedirectToAction(“索引”)而不是PartialView。这解决了我的问题。再次感谢你的提示

我的一个快速建议是将脚本移动到html其余部分的下方。关于良好的开发实践,我有一个快速的建议,就是将脚本移到html的其余部分下面。更多关于良好的开发实践而不是修复。嗨,jrummell,谢谢你的反应。我添加了[HttpPost],并尝试了您建议的[AcceptVerbs(HttpVerbs.Post,HttpVerbs.Get)],但没有成功。我使用Html.BeginForm(“SaveForm”,“Home”)的方式是否可能出错?感谢您的评论jrummell,最终我使用了Ajax.BeginForm而不是Html.BeginForm,在我的操作中添加了[HttpPost]属性,并使用了RedirectToAction(“Index”)而不是PartialView。这解决了我的问题。再次感谢你的提示!嗨,jrummell,谢谢你的反应。我添加了[HttpPost],并尝试了您建议的[AcceptVerbs(HttpVerbs.Post,HttpVerbs.Get)],但没有成功。我使用Html.BeginForm(“SaveForm”,“Home”)的方式是否可能出错?感谢您的评论jrummell,最终我使用了Ajax.BeginForm而不是Html.BeginForm,在我的操作中添加了[HttpPost]属性,并使用了RedirectToAction(“Index”)而不是PartialView。这解决了我的问题。再次感谢你的提示!感谢您的评论,Anthony,最终我使用了Ajax.BeginForm而不是Html.BeginForm,在我的操作中添加了[HttpPost]属性,并使用了RedirectToAction(“索引”)而不是PartialView。这解决了我的问题。再次感谢你的提示!感谢您的评论,Anthony,最终我使用了Ajax.BeginForm而不是Html.BeginForm,在我的操作中添加了[HttpPost]属性,并使用了RedirectToAction(“索引”)而不是PartialView。这解决了我的问题。再次感谢你的提示!感谢您的评论Gauravgat,最终我使用了Ajax.BeginForm而不是Html.BeginForm,在我的操作中添加了[HttpPost]属性,并使用了RedirectToAction(“索引”)而不是PartialView。这解决了我的问题。再次感谢你的提示!感谢您的评论Gauravgat,最终我使用了Ajax.BeginForm而不是Html.BeginForm,在我的操作中添加了[HttpPost]属性,并使用了RedirectToAction(“索引”)而不是PartialView。这解决了我的问题。再次感谢你的提示!