Asp.net mvc 2 MVC2 jquery将对象从视图传递到控制器
我试图将对象myclass传递到视图页面,我面临的问题是null对象正在返回给控制器。有什么建议吗 下面是我的ascx页面中的代码Asp.net mvc 2 MVC2 jquery将对象从视图传递到控制器,asp.net-mvc-2,jquery,Asp.net Mvc 2,Jquery,我试图将对象myclass传递到视图页面,我面临的问题是null对象正在返回给控制器。有什么建议吗 下面是我的ascx页面中的代码 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<myclass>" %> <div class="upModel" style="display:none"> <%= Model%> </div> <script
<%@ 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;
}
$(文档).ready(函数(){
$(“#加载”)。单击(函数(){
试一试{
$.ajax({
类型:“POST”,
url:“Loadmyaction”,
数据:$(“#upModel”).serialize(),
contentType:“应用程序/json;字符集=utf-8”,
成功:函数(数据){
警报(数据);
},
错误:函数(结果){alert(“error”+result);}
});
}
捕捉(错误){
警报(错误描述);
}
});
**在控制器上,这是我的方法**
公共字符串Loadmyaction(字符串对象)
{
字符串str=“”;
返回str;
}
这里obj从视图中变为null。为什么没有数据被传回控制器?您确定它实际上正在命中您的方法吗 您的jquery应该如下所示:
$.ajax({
type: "POST",
url: "<%=Url.Action("Loadmyaction") %>",
data: $("#upModel").serialize(),
dataType: "json",
success: function(data) {
alert(data);
},
error: function(result) {
alert("Error" + result);
}
});
你为什么不使用View.bag或viewdata?我添加了你建议的内容,但仍然返回null。很难说。你应该发布你的类和表单
[HttpPost]
public JsonResult LoadMyAction(MyClass model)
{
return Json("someString");
}