假设cshtml文件中是这样的:
data是json数据。传递到的Action是。那么在Action方法处接收的方式如下:
public ActionResult MyAjax(string val1)
{ string val2 = Request["val2"].ToString(); string val3 = Request.Form["val3"].ToString(); string val4 = Request.Params["val4"].ToString(); return Content("ViewUserControl1"); }
或者接收参数为FormCollection,也有同样的效果。
public ActionResult MyAjax(FormCollection f)
{ string val2 = f["val2"].ToString(); string val3 = f["val3"].ToString(); string val4 = f["val4"].ToString(); return Content("ViewUserControl1"); }
对于上面的例子,我们甚至可以构造出一个class,如下:
public class { public string val1 { set; get; } public string val2 { set; get; } public string val3 { set; get; } public string val4 { set; get; }}
那么就可以设置参数类型为aclass public ActionResult MyAjax( f)
{ return Content(f.val1+f.val2+f.val3+f.val4); }
注意,aclass类的属性名就是json的key的名字,只要符合一致,它就能匹配,不得不说强悍。