Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
How to send data of multiple tables through ajax call?

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; }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda