Umar Abbas
Umar Abbas

Reputation: 4161

Add row in table by data from controller

I want to add a new row in a table When user enter an id in a textBox.

I think the problem is at view side

Controller side

 public JsonResult GetProbyId(Int32 term)
        {
            var results = (from a in db.Products
                           where a.ProID==term
                           select a).FirstOrDefault();
            return Json(results, JsonRequestBehavior.AllowGet);

        }

View side

 <SCRIPT language="javascript">
     function addRow(tableID) {
         var table = document.getElementById(tableID);

         var ids = '@Url.Action("GetProbyId", "Bill", new {term=1 })';

         var cell = row.insertCell(0);       
         cell.innerHTML =ids.Name;
         var cell1 = row.insertCell(1);       
         cell1.innerHTML =ids.Discription;

         </script>
     }

But it doesn't work - please help.

Upvotes: 1

Views: 957

Answers (1)

Amin Saqi
Amin Saqi

Reputation: 18977

Suppose that you receive your data correctly, use the following jquery code to add a row at the bottom of your table:

function addRow(tableID, data) {
    // first, convert your json string to a json object...

    data = $.parseJSON(data);

    // then iterate through it to add rows with its data...

    $.each(data, function(i, item) {
        var html = "<tr><td>" + item.Field1 + "</td>";
        html += "<td>" + item.Field2 + "</td>";
        // ...
        html += "<td>" + item.Fieldn + "</td></tr>";
        $("#" + tableID + " tr:last").after(html);             
    }); 
}

Upvotes: 1

Related Questions