Я пытаюсь передать объект в IActionResult, и это список этих объектов, я использую AJAX (Jquery), но когда я начинаю отладку, объект пришел с нулевыми значениями в мой контроллер.
Я пытаюсь изменить структуру моего кода ajax, но я действительно новичок в этом.
Это мой контроллер, получает контакт
// * модели 1009 *
public class Contact
{
public string firstName { get; set; }
public string lastName { get; set; }
public string phone { get; set; }
public string company { get; set; }
public string email { get; set; }
}
// HomeController
public JsonResult addContact(Contact _contact)
{
bool isThere = false;
foreach(Contact c in this.contacts){
if( c.phone == _contact.phone ){
isThere = true;
}
}
if(!isThere){
this.contacts.Add(_contact);
return Json(new { msg = "ok" });
}
else{
return Json(new { msg = "exists" });
}
}
А это мой .js
function addContact() {
$('#addUserModal').modal("hide");
var _firstName = $('#firstName').val();
var _lastName = $('#lastName').val();
var _email = $('#email').val();
var _phone = $('#phone').val();
var _company = $('#company').val();
//let's create a objet just like our user.cs
var Contact = {
firstName: _firstName,
lastName: _lastName,
email: _email,
phone: _phone,
company: _company
};
console.log(Contact);
$.ajax({
type: "POST",
dataType: "JSON",
contentType: 'application/json; charset=utg-8',
url: "/Home/addContact",
data: JSON.stringify({
'_contact': Contact
}),
error: function () {
toastr.error('system Error, check your connection or re-debug your code', 'Error', {
timeOut: 6000,
positionClass: 'toast-top-full-width'
});
},
success: function (data) {
if (data.msg === "ok") {
toastr.success('OK, Contact added', 'ok', {
timeOut: 6000,
positionClass: 'toast-top-full-width'
});
$("#addContactModal").modal("hide"); //to hide my idModal
$("#listContactTableId").load("/Home/getContacts");
}
else if (data.msg === "exists") {
toastr.error("You're trying to add a user that alreday exists", "Eye!", {
timeOut: 6000,
positionClass: 'toast-top-full-width'
});
}
}
});
}