首先看一下我在控制器Controller中的代码

public JsonResult SearchUnitByID(string ID)
{
Flow_StateUnitDefine unit = new Flow_StateUnitDefine();
unit = FlowAccessor.GetUnitByID(ID);
return this.Json(unit);
}

前端JS请求和返回的代码


function OrgFocusedRowChanged(s, e) {
var ID = s.GetRowKey(s.GetFocusedRowIndex());
if (ID != null) {
$.post("../Flow/SearchUnitByID?MenuID=" + getQueryStringRegExp('MenuID'),
{ ID: s.GetRowKey(s.GetFocusedRowIndex()) },
function (data) {
txtState.SetValue(data.StateName);
txtUnit.SetValue(data.UnitName);
if (data.DataShowMeta == "null") {
memoData.SetValue("");
}
else {
memoData.SetValue(data.DataShowMeta);
}
txtID.SetValue(data.ID);
if (data.Type == "1") {
cboType.SetSelectedIndex(0);
}
else if (data.Type == "2") {
cboType.SetSelectedIndex(1);
}
else {
cboType.SetSelectedIndex(-1);
}
});
}
else {
txtState.SetValue("");
txtUnit.SetValue("");
memoData.SetValue("");
cboType.SetSelectedIndex(-1);
}
}


此时看一下前端Js中用的是Jquery中的post请求,那么在控制器中没什么特别的。

如果前端请求用的是get,那么控制器中要有相应的调整,代码如下:

public JsonResult SearchUnitByID(string ID)
{
Flow_StateUnitDefine unit = new Flow_StateUnitDefine();
unit = FlowAccessor.GetUnitByID(ID);
return this.Json(unit, JsonRequestBehavior.AllowGet);
}


可以看出在return时,this.Json多了一个参数,这就是要和前端是Get请求,还是Post请求相对应的。因为默认的情况下相当于Post请求,所以上面this.Json仅一个参数即可。