日期:2014-05-17  浏览次数:20424 次

Asp.net MVC 后台model接收参数
在 action中我用的model=>people实体类为参数,而ajax传递过来的时候为 name:'zs',age:18(以get方式),但是people总是为null,这种问题可能出现在哪里呢?如何快速的判断这种问题的原因。

------解决方案--------------------
难道4个三角了还不会调试、单元测试、排除法、替换法排错,再不济,把你的问题说说清楚?
------解决方案--------------------
引用:
在 action中我用的model=>people实体类为参数,而ajax传递过来的时候为 name:'zs',age:18(以get方式),但是people总是为null,这种问题可能出现在哪里呢?如何快速的判断这种问题的原因。

ajax 处理的格式是json.

(问题说得不清楚, 不好猜)
------解决方案--------------------
get方式有安全问题 尽量用post吧  如果要用get 要加JsonRequestBehavior.AllowGet  
例如:return Json(obj,JsonRequestBehavior.AllowGet);
------解决方案--------------------
引用:

受到批评了。。。

Public ActionResult AddPeople(People newPeople)
{
    //执行这个action的时候 newPeople 为null
    ...
}

------------
js:
$.get("DoSt/AddPeople", { name: "John", age: "18" } );

前台用 httpwatch 监控访问正常,调试也能进入到请求的action,但是作为newPeople总是为null。

没想到我发的问题这么难以理解...不好意思

JsonRequestBehavior.AllowGet
------解决方案--------------------
你用 request["name"] 看看能不能取到值
------解决方案--------------------

应该这样
Public ActionResult AddPeople(string  name,int age)
{
    var people=new newPeople{Name=name,Age=age};
    //执行这个action的时候 newPeople 为null
    ...
}

------------
js:
$.get("DoSt/AddPeople", { name: "John", age: "18" } );