mvc ajax控制器无反应,关于ASP.NET MVC 3:无法通过AJAX将数据传递到控制器

我有以下模型类:

[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)

{

}

谢谢,但对我的情况没有帮助:(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值