【问题标题】:How to Receive Ajax Data values from Controller in asp mvc如何在asp mvc中从控制器接收Ajax数据值
【发布时间】:2017-05-07 11:58:05
【问题描述】:

使用以下脚本,我试图访问使用 ajax 函数中的数据发送的变量,但我不能。

<script>
$('#inline-username').click(function () {
    var comments = $('#inline-username').val();
    //var selectedId = $('#hdnSelectedId').val();

    $.ajax({
        url: '@Url.Action("UpdateOrder")', // to get the right path to controller from TableRoutes of Asp.Net MVC
        dataType: "json", //to work with json format
        type: "POST", //to do a post request
        contentType: 'application/json; charset=utf-8', //define a contentType of your request
        cache: false, //avoid caching results
        data: { test: $(this).text() }, // here you can pass arguments to your request if you need
        success: function (data) {
            // data is your result from controller
            if (data.success) {
                alert(data.message);
            }
        },
        error: function (xhr) {
            alert('error');
        }
    });
});

这是控制器中的动作

public ActionResult UpdateOrder()
    {
        // some code
        var test = Request.Form["test"];
        return Json(new { success = true, message = "Order updated successfully" }, JsonRequestBehavior.AllowGet);
    }

我试过Request.Form["test"],但它的值为空。我应该如何接收data的对象?

【问题讨论】:

  • 您需要在 url: '@Url.Action("UpdateOrder","ControllerName")' 中指定控制器名称, 并使用 [HttpPost] 属性装饰您的操作.

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


【解决方案1】:

您的 ActionResult 是 GET 并且您的 ActionResult 没有输入参数,所以要么更改这些参数,要么见下文:

<script>
$('#inline-username').click(function () {
    var comments = $('#inline-username').val();
    //var selectedId = $('#hdnSelectedId').val();

    $.ajax({
        url: /ControllerName/ActionName
        dataType: "json",
        type: "GET", 
        contentType: 'application/json; charset=utf-8', //define a contentType of your request
        cache: false, 
        data: { test: comments  },
        success: function (data) {
            // data is your result from controller
            if (data.success) {
                alert(data.message);
            }
        },
        error: function (xhr) {
            alert('error');
        }
    });
});

然后在你的控制器中:

public ActionResult UpdateOrder(string test)
    {
        // some code
        return Json(new { success = true, message = "Order updated successfully" }, JsonRequestBehavior.AllowGet);
    }

更新

请记住,如果您想使用POST,那么您调用的操作必须是[HttpPost],例如:

[HttpPost]
public ActionResult Example()

如果您的 ActionResult 上方没有 HTTP,则默认情况下为 Get

【讨论】:

  • @MohammedAlAamri 很高兴它成功了 :)
【解决方案2】:

action 方法应该用 [HttpPost] 属性修饰。您是否使用调试器来确保您确实调用了该方法?

您总是可以在 C# 中定义一个视图模型,然后将其作为您的 post 方法的参数接受 - 只要值的名称与您的模型相同,Asp.MVC 就会为您解析发布数据。

【讨论】:

    【解决方案3】:

    您是否使用 [HttpPost] 属性标记了您的操作方法。 ?

    【讨论】:

      【解决方案4】:

      这篇文章对我帮助很大。我执行了 GET 但 POST 仅使用 [HttpPost] 引发了内部服务器错误:

      [HttpPost]
      public ActionResult SaveOrder(int id, string test, string test2)
      

      所以我不得不用 JSON.stringify 设置参数数据,它工作。我对 POST 的完整 ajax 请求:

      $.ajax({
          url: "/Home/SaveOrder",
          dataType: "json",
          type: "POST", 
          contentType: 'application/json; charset=utf-8', //define a contentType of your request
          cache: false, 
          data: JSON.stringify({ id:2, test: "test3", test2: "msj3" }),
          success: function (data) {
              // data is your result from controller
              if (data.success) {
                  alert(data.message);
              }
          },
          error: function (xhr) {
              alert('error');
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-17
        • 1970-01-01
        • 1970-01-01
        • 2017-01-20
        • 2019-11-08
        • 1970-01-01
        • 2019-07-21
        • 1970-01-01
        相关资源
        最近更新 更多