2014-10-19 198 views
0

我正在将数据从JavaScript应用程序发送到MVC5控制器,但是当数据提交到Submit controller操作时,它永远不会被调用。我有创建以下JSON对象一些很简单的制图员:未从JSON AJAX调用MVC5控制器操作Post

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":"[email protected]", 
    "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()然后调用它,为什么?

  • 从上面,似乎控制器框架无法匹配的参数 - 任何想法我做错了请吗?

  • 如何在MVC控制器上启用调试,以便我可以看到发生了什么?

回答

1

你将不得不做出两处修改: 字符串化您的JSON如下:

$.ajax({ 
    cache: false, 
    url: url, 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    data: JSON.stringify(item), 
    type: 'POST', 
    success: function (data, textStatus, jqXHR) {   
    }, 
    error: function (data, textStatus, jqXHR) { 
    } 
}); 

其次,只是标注您的方法与[HttpPost]如下:

[HttpPost] 
public JsonResult Submit(string[] Skus, ContactDto Contact) 
{ 
    return Json(new { success = true, message = "Some message" }); 
} 
2

四件事情你必须使用ajax调用进行检查,
1.如果使用JavaScript对象,则必须在传递之前将对象串联起来。
2.动作方法的动作动词应与您的ajax调用的类型相同,如果POST则动作方法应由动作动词[HttpPost]装饰。
3.始终在@ url.Action(“action”,“controller”)中使用ajax中url的相对路径。
4.您的操作方法方法的输入参数应与json对象参数匹配(严格来说,区分大小写)。

为了进行调试,您可以在浏览器中使用firebug插件,以便您可以查看通过网络发送的内容,或者在该检查网络选项卡中按F12以查找调试工具。