mvc5 ajax post json,未从JSON AJAX中调用MVC5控制器操作

我正在将数据从javascript应用程序发送到MVC5控制器,但是,当数据提交到Submit

controller操作时,它永远不会被调用。我有一些非常简单的映射器,它们创建以下JSON对象:

function mapContactDto(vm)

{

var contactDto = {};

contactDto.firstName = vm.firstName();

contactDto.lastName = vm.lastName();

contactDto.companyName = vm.companyName();

contactDto.emailAddress = vm.emailAddress();

contactDto.phonePrimary = vm.phonePrimary();

contactDto.phoneSecondary = vm.phoneSecondary();

contactDto.address1 = vm.address1();

contactDto.address2 = vm.address2();

contactDto.city = vm.city();

contactDto.postalCode = vm.postalCode();

contactDto.country = vm.country();

return contactDto;

}

function mapCartItems(vm)

{

var cartItemsDto = new Array();

$.each(vm.selectedOptions(), function (index, step, array) {

var sku = step.selection().sku;

if (sku !== "0") {

cartItemsDto.push(sku);

}

});

return cartItemsDto;

}

/* if i dump the object that is sent to the server with `console.log(JSON.stringify(item))` I get:

{

"skus": ["1001","8GB","201"],

"contact": {

"firstName":"Jon",

"lastName":"Doe",

"companyName":"Yup my company",

"emailAddress":"contact@me.com",

"phonePrimary":"012111 231",

"phoneSecondary":"",

"address1":"1 Billing Way",

"address2":"Navigation House",

"city":"London",

"postalCode":"112211",

"country":"GB"

}

}

*/

然后,我使用以下代码发送数据:

var contactDto = mapContactDto(self.billingVm());

var cartItemsDto = mapCartItems(self.configurationVm());

var req = new XMLHttpRequest();

req.open('HEAD', document.location, false);

req.send(null);

var item = {

skus: mapCartItems(cartItemsVm()),

contact: mapContactDto(billingVm())

};

var url = '/Knockout/Submit';

$.ajax({

cache: false,

url: url,

contentType: 'application/json; charset=utf-8',

dataType: 'json',

data: item,

type: 'POST',

success: function (data, textStatus, jqXHR) {

},

error: function (data, textStatus, jqXHR) {

}

});

我的控制器代码如下:

public JsonResult Submit(string[] Skus, ContactDto Contact)

{

return Json(new { success = true, message = "Some message" });

}

/* and MVC models are: */

public class ContactDto

{

public string FirstName { get; set; }

public string LastName { get; set; }

public string CompanyName { get; set; }

public string EmailAddress { get; set; }

public string PhonePrimary { get; set; }

public string PhoneSecondary { get; set; }

public string Address1 { get; set; }

public string Address2 { get; set; }

public string City { get; set; }

public string PostalCode { get; set; }

public string Country { get; set; }

}

我有以下问题:

但是,从不会调用Submit,但是,如果我注释掉了控制器参数,Submit()那么它将被调用,这是为什么呢?

从上面看来,控制器框架似乎无法匹配参数-知道我在做什么错吗?

如何在MVC控制器上启用调试,以便了解发生了什么情况?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值