我有以下模型类:
[Serializable]
public class SearchHomeModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Company { get; set; }
public string Title { get; set; }
}
我有以下控制器代码:
public ActionResult DataTableUserList(SearchHomeModel search, UserListType type, int iDisplayStart, int iDisplayLength, string sEcho)
和以下客户端部分:
var search = {};
search.FirstName = 'aa';
search.LastName = 'bb';
search.Company = 'kkk';
search.Title = 'aaaawww';
fnServerData: function (sSource, aoData, fnCallback) {
aoData.push({"name":"type","value":"All" });
aoData.push({"name":"search","value": search });
$.ajax({
dataType: 'json',
type:"POST",
url: sSource,
data: aoData,
但是我在服务器端查看调试器,看到type =" All"(正确),但是搜索为null。 为什么以及如何将数据从ajax传递到搜索对象?
显示您的模型代码将很有帮助。
type ="All"不是string时如何? 查看Request.Params,看看控制器得到了什么。
"显示模型代码会有所帮助"-添加
您可能会或可能不会觉得这有用,但是我这样做是为了直接从控制器来回传递JSON。 在玩Knockout.js时,我曾在这里遇到过这种方法。
我创建了这个属性
public class FromJsonAttribute : CustomModelBinderAttribute
{
private readonly static JavaScriptSerializer serializer = new JavaScriptSerializer();
public override IModelBinder GetBinder()
{
return new JsonModelBinder();
}
private class JsonModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var stringified = controllerContext.HttpContext.Request[bindingContext.ModelName];
if (string.IsNullOrEmpty(stringified))
return null;
return serializer.Deserialize(stringified, bindingContext.ModelType);
}
}
}
这是我的控制器代码:
[HttpPost]
public ActionResult Gifts([FromJson] List gifts, [FromJson] string guid)
{
}
谢谢,但对我的情况没有帮助:(