0

我需要使用 POST 将用户的表信息发送到 MVC 控制器(将其保存在服务器的会话中)。

我看到使用嗅探器成功传递了 POST 的信息,但它永远不会到达实际的 MVC 控制器(它永远不会到达断点!)。

我的代码:

<script type="text/javascript">
    $(document).ready(function () {
        $("#btnSave").click(function () {
            var tableStr = $("#divTable").html();
            $.post("Home/Save/", tableStr, function (data) {
                if (data) {
                    alert("Great success!");
                }
                else {
                    alert("Fail!");
                }
            });
            return false;
        });
    });
</script>

<h2><%: ViewBag.Message %></h2>
<%=Ajax.ActionLink("Show Users", "LoadUsers", new AjaxOptions() { InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace, UpdateTargetId = "divTable" }) %>

<center><div id="divTable"></div></center>
<input type="button" id="btnSave" value="Save" />

控制器:

    [HttpPost]
    public ActionResult Save(string tableHtml)
    {
        Session["TableStr"] = tableHtml;
        return new JsonResult() { Data = true };
    }

我究竟做错了什么??谷歌搜索了几个小时!

有没有更简单的方法可以将此信息传递给我的服务器会话?这必须是 AJAX(A-同步调用)。

4

4 回答 4

2

如果您正在发帖,您确实需要[HttpPost],即使它仍然不起作用,也不要删除它。您还需要确保返回 JSON 数据。

假设您这样做了并且您的控制器实际上被正确引用,您可以尝试显式声明您的 AJAX 元素以确保所有内容都正确传递,包括您的有效负载的名称:

$("#form").submit(function () {
    $.ajax({           
       type: 'POST',
       dataType: 'json',
       url: '/Home/Save/', 
       data: { tableHtml: tableStr }, 
       success: function (data) {
            ...
       }
    });
});

[HttpPost]
public JsonResult Save(string tableHtml)
{
    Session["TableStr"] = tableHtml;
    return Json(new { Data = "true" });
}
于 2012-06-18T14:52:16.863 回答
1

看起来你缺少 /,试试这个

$.post("/Home/Save/", tableStr , function (data) {
            if (data) {
                alert("Great success!");
            }
            else {
                alert("Fail!");
            }
        });

这是一篇关于这样做的博客文章http://bob-the-janitor.blogspot.com/2011/11/more-ajax-with-mvc-using-partial-views.html

于 2012-06-18T13:58:01.993 回答
1

我认为你需要用 httppost 装饰你的方法:

[HttpPost]
public ActionResult Save(string tableHtml)
{
    Session["TableStr"] = tableHtml;

    return new JsonResult() { Data = true };
}

另外,请确保路径对您的控制器是正确的。如果您确定,请使用$.ajax调用而不是$.post使用错误回调来查看问题所在。

于 2012-06-18T14:42:16.213 回答
0

I see a few things wrong with this.

  1. You are calling $("#Form").Submit()...., you have no element with the ID form. Try putting <form id="theForm"> and changing your call to $("#theForm").submit()...

  2. You are setting your tableStr variable when the page loads. Looking at your example, this is empty. Then, you call an Ajax actionlink to populate the div. However, you never update your tableStr variable again with the AJAX received content. You will be sending an empty string to the server once you get this to work.

  3. Why are you using a FORM element to do this? Forms are typically used to collect information from the user to submit to the server, which in this example, you are not collecting data, only sending the content of a div. You could have a button without the form to do the same thing and will grab whatever is in the "DivTable" element at the time of the button click:

Javascript

$(document).ready(function () {
    $("#myButton").click(function () {
       var tableStr = $("#divTable").html();
       $.ajax({           
          type: 'POST',
          dataType: 'json',
          url: '/Home/Save/', 
          data: { tableHtml: tableStr }, 
          success: function (data) {
             alert("Great success!");
           },
          error: function(data){
             alert('fail');
          }
       });
    });
  });

Controller

[HttpPost]
public ActionResult Save(string tableHtml)
{
    Session["TableStr"] = tableHtml;

    return new JsonResult() { Data = true };
}

HTML

<center><div id="divTable"></div></center>
<input type="button" id="myButton" value="Save some stuff"/>

4.Why is this necessary anyways? Looking at this code, you appear to be populating a list from an Ajax call to the server (Using the Ajax.ActionLink), then resubmitting that list back to the server to store in session? If that is right, just store the information in session when you call your Ajax link.

public ActionResult LoadUsers()
    {
        var UserInfoHtmlString = GetYourUserInfo();
        Session["TableStr"] = UserInfoHtmlString ;
        return UserInfoHtmlString;
    }
于 2012-06-18T18:26:01.827 回答