0

i need your help please. I have created a JQXGrid with MVC from two different tables "Prodotti" and "Scheda". I load data from controller with entity framework in this way:

public JsonResult GetProducts()
{
    try
    {
        var dbResult = db.PartnerProducts.ToList();
        var products = from e in dbResult
                       select new
                       {
                           e.Id,
                           e.PartnerId, 
                           e.Title,
                           e.Price,
                           e.Quantity,
                           e.Status,
                           e.DateCreated,   
                           e.Photo,
                           e.ShortDescription,
                           e.LongDescription,
                           e.cat,   
                           e.lotto, 
                           e.nomeproduttore,    
                           e.origine,
                           e.idcategoria,   
                           e.apezzo,
                           e.pesomedio,
                           e.prezzoscontato,
                           e.IdSchedaProdotto, 
                           e.Priority,  
                           e.PartnerOwner, 
                           e.Deperibile,
                           e.inbustati,
                           e.confezionati,
                           nomeCategoria = e.categoriaprodottopersonalizzato?.nome,
                           sottotitolo = e.SchedaProdotto?.Sottotitolo,
                           provenienza = e.SchedaProdotto?.Provenienza,
                           curiosita = e.SchedaProdotto?.Curiosita,
                           proprieta = e.SchedaProdotto?.Proprieta,
                           periodo = e.SchedaProdotto?.Periodo,
                           conservazione = e.SchedaProdotto?.Conservazione,
                           foto = e.SchedaProdotto?.Foto,   
                           titolo = e.SchedaProdotto?.Titolo,
                           visibile = e.SchedaProdotto?.Visibile,
                           link = e.SchedaProdotto?.Link,   
                           viaAerea = e.SchedaProdotto?.ViaAerea,
                           nome = e.SchedaProdotto?.Nome
                       };
      }

      return Json(products, JsonRequestBehavior.AllowGet);
}

And i load all data in JQuery table with this code:

datafields: [{ name: 'Id' },
                { name: 'PartnerId' },
                { name: 'Title' },
                { name: 'Price' },
                { name: 'Quantity' },
                { name: 'Status' },
                { name: 'DateCreated' },
                { name: 'Photo' },
                { name: 'ShortDescription' },
                { name: 'LongDescription' },
                { name: 'cat' },
                { name: 'lotto' },
                { name: 'nomeproduttore' },
                { name: 'origine' },
                { name: 'idcategoria' },
                { name: 'apezzo' },
                { name: 'pesomedio' },
                { name: 'prezzoscontato' },
                { name: 'IdSchedaProdotto' },
                { name: 'Priority' },
                { name: 'PartnerOwner' },
                { name: 'Deperibile' },
                { name: 'inbustati' },
                { name: 'confezionati' },
                { name: 'nomeCategoria' },
                { name: 'sottotitolo' },
                { name: 'provenienza' },
                { name: 'curiosita' },
                { name: 'proprieta' },
                { name: 'periodo' },
                { name: 'conservazione' },
                { name: 'foto' },
                { name: 'titolo' },
                { name: 'visibile' },
                { name: 'link' },
                { name: 'viaAerea' },
                { name: 'nome' }

And the load of data works well. Now the problem is when i want to add new data...i read data from column with this function:

 addrow: function (rowid, rowdata, position, commit) {
            // synchronize with the server - send insert command
            // call commit with parameter true if the synchronization with the server is successful
            // and with parameter false if the synchronization failed.
            // you can pass additional argument to the commit callback which represents the new ID if it is generated from a DB.
            var data = "add=true&Id=" + rowdata.Id + "&PartnerId=" + rowdata.PartnerId +  "&Title=" + rowdata.Title + "&Price=" + rowdata.Price + "&Quantity=" + rowdata.Quantity + "&Status=" + rowdata.Status;
            data = data + "&Photo=" + rowdata.Photo + "&cat=" + rowdata.cat + "&lotto=" + rowdata.lotto + "&origine=" + rowdata.origine + "&idcategoria=" + rowdata.idcategoria;
            data = data + "&apezzo=" + rowdata.apezzo + "&pesomedio=" + rowdata.pesomedio + "&prezzoscontato=" + rowdata.prezzoscontato;
            data = data + "&IdSchedaProdotto=" + rowdata.IdSchedaProdotto + "&Deperibile=" + rowdata.Deperibile + "&confezionati=" + rowdata.confezionati + "&nomeCategoria=" + rowdata.nomeCategoria;
            data = data + "&provenienza=" + rowdata.provenienza + "&viaAerea=" + rowdata.viaAerea + "&periodo=" + rowdata.periodo + "&curiosita=" + rowdata.curiosita;
            data = data + "&proprieta=" + rowdata.proprieta + "&conservazione=";
            $.ajax({
                dataType: 'json',
                url: 'AddProducts',
                data: data,
                success: function (data, status, xhr) {
                    // update command is executed.
                }
            });

And i want to realobarate this function from server here:

public bool AddProducts(PartnerProduct product)

The AddProducts function work fine but in the object Product i can retrieve only value from table Product, such as title or price...How can i save in Jquery values for table "scheda" that are empty in this server-side function?

For example i load data with

titolo = e.SchedaProdotto?.Provenienza,

Pass it to server side with

"&provenienza=" + rowdata.provenienza

but the value in server side of 'Scheda.Provenienza' is empty

Please help me...is very important

Pedro Benevides
  • 1,970
  • 18
  • 19
user5297740
  • 147
  • 2
  • 12

1 Answers1

1

DISCLAIMER: This answer may not solve the problem, but I am forced to post it as an answer because I don't have enough reputation to post comments.

What I want you to do first is define AJAX request type. So, you need this:

$.ajax({
    type: 'POST', // or, type: 'GET', depending on your controller. If you ask me, POST is the right request type to use here, because you want to submit data.
    dataType: 'json',
    url: 'AddProducts', // in what controller is this function located? Is this the right path to the function?
    data: data,
    success: function (data, status, xhr) {
        // update command is executed.
    }
});

Now, the second issue is in the controllers parameter, where you expect to receive PartnerProduct model, but from AJAX you don't send any model. In other words, from AJAX you are sending an object, and C# does not how to convert it in PartnerProduct. Because of this reason, I will ask you to check out the following links, which I think will solve your problem:

If this does not help, ping me in comment so we can look further step by step.

Community
  • 1
  • 1
  • Hi and thanks for your reply. I think that the function addProducts is correctly located...in debug mode i can access it correctly. The only problem is how to pass the value of data in this example. I have only one object for time and the PartnerProduct object in the function in the controller has the value for the first table correctly definited from the data attribute. So, i don't understand how to pass value also for the second foreign table related with entity framework, but if i have a mode to pass an array of data is the same for me. Thank you – user5297740 Jan 11 '16 at 06:08
  • Have you checked the links I provided? It is explained there with codes how to do it, I don't want to repeat the same code over again. Please try what is written there, and let me know if that worked. –  Jan 11 '16 at 10:19