I am sending data to a controller using JQuery, but controller is not recieveing any data.
I want to post data of two tables, the fields are dynamically created and some are in the html.
Here's a sample of my code:
{
coursesarray.push($("#coursename").val());
for (var i = 1; i < counter; i++)
{
var courses = $('#coursename' + i).val();
coursesarray.push(courses);
}
obj.tbl_course = coursesarray;
obj.Name = Name;
$.ajax(
{
type: "POST",
datatype: "json",
contenttype: "application/json/",
url: "/Demo/SaveData/",
data: JSON.stringify(obj),
success: savesuccess,
error:saveerror
});
});
Controller Method
public JsonResult SaveData(tbl_name tblobj)
{
db.tbl_name.Add(tblobj);
db.SaveChanges();
foreach (var a in tblobj.tbl_course)
{
db.tbl_course.Add(a);
db.SaveChanges();
}
return Json("success", JsonRequestBehavior.AllowGet);
}
And model is
public partial class tbl_name
{
public int PkId { get; set; }
public string Name { get; set; }
public virtual ICollection<tbl_course> tbl_course { get; set; }
}
and second model is
public partial class tbl_course
{
public int Pkid { get; set; }
public Nullable<int> FkId { get; set; }
public string Coursename { get; set; }
public string CourseTeacher { get; set; }
public virtual tbl_name tbl_name { get; set; }
}
I have my class
public class Customer
{
public string id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public List<Shop> Shops { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
And my Shop class is like this
public class Shop
{
public string Name { get; set; }
public string Address { get; set; }
public string AddressContd { get; set; }
public ShopContacts ShopContacts { get; set; }
}
And my ShopContacts class is just like a simple class, including fname, lname etc properties.
My controller method is like this
[HttpPost]
public IActionResult Post(Customer Data)
{
...
}
And my jQuery/Ajax is like this,
var contacts = {
FirstName: $("#firstName").val(),
LastName: $("#lastName").val(),
Email: $("#email").val(),
};
var ShopsData = [{
Name: $("#ptLocAddr").val(),
Address: $("#ptLocAddr").val(),
AddressContd: $("#ptLocAddrContd").val(),
Country: $("#ptLocCountry").val(),
State: $("#ptLocState").val(),
City: $("#ptLocCity").val(),
Zip: $("#zip").val(),
FacilityType: $("#ptFacility").val(),
Radius: $("#ptShopRadius").val(),
ShopContacts: contacts
}]
var Data = {
id: 0,
Shops: ShopsData
}
$.ajax({
"url": "http://localhost:63025/api/values/",
"type": "Post",
"data":Data,
success: function (d) {
console.log(d);
}
})
And it's working as expected. In your case just add the values using push() method in jQuery and follow the way I'm doing.
var myArray = [];
Here you can do the .each() and push the value to the array like this below:
myArray.push(yourForEachData);
Then
var myData = {
RuleID: $('#hidRuleId').val(),
ProcessDefnID: $('#hidProcessDefnId').val(),
RuleName: $('#txtEditRuleName').val(),
Conditions: Conditions,
...
}
And in your ajax method just place the myData in data field, like this
{
...,
data: myData,
...,
}
That's it! It will work definitely. Just change the class name, variable name and properties to match with your requirements.