1

次の問題があります (解決策を探すのに何時間も費やしています)。

「作成」ボタンには、「ホーム」コントローラで「テスト」アクションを呼び出すクリック イベントがあります。

すべて正常に動作します。

「保存」ボタンを押してフォームを送信すると、うまくいきます。

しかし、フォームを送信した後、[作成] ボタンが機能しなくなりました。「作成」ボタンにはクリックイベントがありますが、「テスト」アクションには到達できませんか?

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" />
  }

HomeController.cs

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        return View();
    }

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

    public ActionResult SaveForm()
    {
        return View("Index");
    }
    public ActionResult About()
    {
        return View();
    }
}
4

4 に答える 4

1

すべてのアクションは GET のみです。[HttpPost]POST アクションに(POST のみ) または[AcceptVerbs(HttpVerbs.Post, HttpVerbs.Get)](GET または POST) 属性を追加します。

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

[HttpPost]
public ActionResult SaveForm()
{
    return View("Index");
}
于 2012-11-30T15:39:49.363 に答える
0

最終的に、Html.BeginFormの代わりにAjax.BeginFormを使用し、アクションに[HttpPost]属性を追加し、PartialViewの代わりにRedirectToAction( "Index")を使用しました。それで私の問題は解決しました。ヒントをありがとう!

于 2012-12-05T13:35:57.910 に答える
0

エンティティを作成するには、あなたの場合のように、ポストバックまたは ajax のいずれかによって、データをサーバーに送信する必要があります。今、あなたのコードにはいくつかの矛盾があります:)

1. Why are you calling one action as form action and another through
   ajax? Because since your button will post back, your ajax call won't
   fire the success and error handlers. To solve this either use <button> or 

$("#create").click(function (e) { e.preventDefault();//Some code});

2. If you want to use ajax, write it like this. Also write [HttpPost] on 
   your action. This indicates that this action can be called by post requests.
   This is a required step, whether or not you are using ajax. 

これで問題が解決したことを願っています。

于 2012-12-03T15:16:34.113 に答える
0

2つの問題があります:

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>

それが機能するかどうか教えてください:)

于 2012-11-30T15:46:44.300 に答える