【问题标题】:MVC2 jquery pass object from view to controllerMVC2 jquery将对象从视图传递到控制器
【发布时间】:2012-05-31 02:21:46
【问题描述】:

我试图将对象 myclass 传递给视图页面,我面临的问题是 null 对象被返回给控制器。有什么指点吗?

下面是我的 ascx 页面中的代码

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<myclass>" %>

<div class="upModel" style="display:none">
<%= Model%>
</div>

<script type="text/javascript">
  $(document).ready(function () {

    $("#load").click(function () {
                try {

                   $.ajax({
                            type: "POST",
                            url: "Loadmyaction",
                            data:  $("#upModel").serialize(),  
                            contentType: "application/json; charset=utf-8", 
                            success: function(data) { 
                             alert(data); 

                             },
                            error: function(result) { alert("Error" + result); }
                        });
                }
                catch (err) {
                    alert(err.description);
                }
          });
     </script>



    **On controller, this is my method**

      public string Loadmyaction(string obj)
            {
    string str = "";
    return str;
    }

这里 obj 从视图中获取 null。为什么没有数据被传回控制器?

【问题讨论】:

  • 你为什么不使用 View.bag 或 viewData

标签: asp.net-mvc-2 jquery


【解决方案1】:

你确定它真的符合你的方法吗?

您的 jquery 应该如下所示:

$.ajax({
    type: "POST",
    url: "<%=Url.Action("Loadmyaction") %>",
    data: $("#upModel").serialize(),
    dataType: "json",
    success: function(data) { 
        alert(data); 
    },
    error: function(result) { 
        alert("Error" + result); 
    }
});

尝试将您的参数类型更改为myClass,无论它是什么。

[HttpPost]
public JsonResult LoadMyAction(MyClass model)
{
    return Json("someString");
}

【讨论】:

  • 我添加了你的建议,但它仍然返回 null。
  • 很难说。你应该发布你的班级和表格
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
相关资源
最近更新 更多