【问题标题】:Issue while calling action method with ajax contentType使用 ajax contentType 调用操作方法时出现问题
【发布时间】:2016-11-26 19:53:41
【问题描述】:

我有一个需要通过 Ajax 回发的应用程序。拨打ActionMethod 时遇到问题。下面是代码。

控制器:

[HttpGet]
    public ActionResult Index()
    {
        return View();
    }

    public JsonResult AjaxTest(object name)
    {
        return Json("Welcome" + name, JsonRequestBehavior.AllowGet);
    }

Cstml:

<form name="frmTest" method="POST">
@Html.Label("Your name Please")
@Html.TextBox("username")
<input class="btn btn-success btn-lg btn-block" type="submit" value="Login">

jQuery :

$(function () {
    $("form[name='frmTest']").submit(function (e) {
        var name = $('#username').val();
      $.ajax(
      {
          type: "POST",
          //contentType: "application/json; charset=utf-8",
          url: "MyTest/AjaxTest",
          dataType: "json",
          data: { name:name },
          success: function (data, status) {
              alert("Pass"+data);
          },
          error: function () {
              alert("Fail");
          }
      });
  });

});

通过使用上面的代码断点命中AjaxTest的参数。当我使用contentType: "application/json; charset=utf-8", 时,页面回发而不打断点。 但是这两种情况我都收到了失败警报。找不到问题,

【问题讨论】:

  • 请问你为什么要一路到服务器上简单地在名字中加上“欢迎”?
  • 您在发送一些 ajax 时尝试提交表单,我认为这是一个或另一个,而不是同时。
  • @CodingYoshi thnx,这是我的主应用的演示应用
  • @JorgeF thnx,在 AjaxTest 中我想将数据插入数据库,那么最好的方法是什么。
  • @JorgeF 事件处理程序发生在实际提交表单之前,因此可以这样做。

标签: c# jquery ajax asp.net-mvc


【解决方案1】:

我不确定您要做什么,但由于在您所说的其中一个 cmets 中您将把内容保存到数据库中,通常在 ASP MVC 中会这样做。

为您将使用 AJAX 发布的任何内容创建一个类。例如,在这个例子中,我创建了一个名为 Person 的类

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

然后在你的表单中你可以有这样的东西:

@{
    ViewBag.Title = "Test";
}

<form name="frmTest" method="POST">
    @Html.Label("Your name Please")
    @Html.TextBox("username")
    <input class="btn btn-success btn-lg btn-block" type="submit" value="Login">
</form>

<script>
    $(function () {
        $("form[name='frmTest']").submit(function (e) {
            var name = $('#username').val();
            $.ajax(
            {
                type: "POST",
                //contentType: "application/json; charset=utf-8",
                url: "AjaxTest",
                dataType: "json",
                data: { "Age": 55, "Name": name },
                success: function (data, status) {
                    alert("Pass" + data);
                },
                error: function (ex) {
                    alert("Fail" + ex);
                }
            });
        });
    });

</script>

然后有一个 JSON 将被提交到的操作。对于上面的,动作会是这样的:

public JsonResult AjaxTest(Person person)
{
    return Json("Welcome" +  person.Name, JsonRequestBehavior.AllowGet);
}

ASP MVC 将为您处理绑定。另请注意,我发布的 url 是“AjaxTest”,它将发布到为表单提供服务的控制器的 AjaxTest 操作方法。

如果你有一个像下面这样的控制器,那么上面所有的都可以工作。

public class AccountController : Controller
{
    // This returns the view with the form
    public ActionResult Test()
    {
        return View();
    }
    public JsonResult AjaxTest(Person person)
    {
        return Json("Welcome" +  person.Name, JsonRequestBehavior.AllowGet);
    }
}

【讨论】:

  • 你的意思是它按照你的例外工作是什么意思?你的意思是期待吗?是的 e.preventDefault 将停止提交表单,因为这是默认行为
  • 对不起期待*
猜你喜欢
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-10
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多