Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Bind JSON return result value in webgird on ASP.net MVC4
    primarykey
    data
    text
    <p>I want to bind <code>Datatable</code> values in webgrid using Json. I used <code>DropDownlist</code> to select Name of person dependent on those names i want to bind it in <code>webgrid.Now</code> i used some codes but unable t bind data in grid.</p> <p>My Code Is:</p> <p>View</p> <pre><code>&lt;div id="divgrid" style="margin-top: 15px;"&gt; @{ var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent"); grid.Pager(WebGridPagerModes.NextPrevious);} &lt;div id="gridContent"&gt; @grid.GetHtml( tableStyle: "webgrid-table", headerStyle: "webgrid-header", footerStyle: "webgrid-footer", alternatingRowStyle: "webgrid-alternating-row", selectedRowStyle: "webgrid-selected-row", rowStyle: "webgrid-row-style", columns: grid.Columns( grid.Column("Id", "ID", style: "id"), grid.Column("Cust_Name", "Cust Name", style: "PName"), grid.Column("Pay_Amount", "Pay_Amount", style: "ICode"), grid.Column("Pay_Mode", "Pay_Mode", style: "IName"), grid.Column("Bank_Name", "Bank_Name", style: "Weight"), grid.Column("Bank_Address", " Bank_Address", style: "MakingCharge"), grid.Column("ChequeNo", "ChequeNo", style: "Certification"), grid.Column("Cheque_Date", " Cheque_Date", style: "Price"), //grid.Column("Cheque_Date", "Cheque_Date", format: item =&gt; ((item.Cheque_Date == null) ? "" : item.Cheque_Date.ToString("dd/MM/yyyy")), style: "name"), grid.Column("Date", "Date", style: "Price"), grid.Column("Edit", "", @&lt;a href='../Admin/EditReceipt?id=@item.ID' &gt;&lt;img src="~/Images/edit.png" alt='Edit' /&gt;&lt;/a&gt;, style: "width:10%"), grid.Column(header: "Delete", format: @&lt;text&gt;&lt;a href="@Url.Action("DeleteReceipt", "Admin", new { Id = item.ID })" onclick="javascript:return confirm('Are you sure you'd like to delete this product?');"&gt;&lt;img src="../Images/delete.png" alt='Delete' /&gt;&lt;/a&gt;&lt;/text&gt;) )) @if (grid.HasSelection) { Receipt = (JewellaryWeb.Models.Receipt)grid.Rows[grid.SelectedIndex].Value; &lt;b&gt;Id&lt;/b&gt; @Receipt.Id&lt;br /&gt; &lt;b&gt;Name&lt;/b&gt; @Receipt.Cust_Name&lt;br /&gt; &lt;b&gt;Amount&lt;/b&gt; @Receipt.Pay_Amount&lt;br /&gt; &lt;b&gt;PayMode&lt;/b&gt; @Receipt.Pay_Mode&lt;br /&gt; &lt;b&gt;BankName&lt;/b&gt; @Receipt.Bank_Name&lt;br /&gt; &lt;b&gt;BankAddress&lt;/b&gt; @Receipt.Bank_Address&lt;br /&gt; &lt;b&gt;ChequeNumber&lt;/b&gt; @Receipt.ChequeNo&lt;br /&gt; &lt;b&gt;ChequeDate&lt;/b&gt; @Receipt.Cheque_Date&lt;br /&gt; } &lt;/div&gt; </code></pre> <p>Script:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function () { $("#Cust_Id").change(function () { firstDDLValue = $("#Cust_Id").val(); // alert(firstDDLValue); $.post('@Url.Action("SelectCustomerByID", "Admin")', { secondValue: firstDDLValue }, function (ReceiptList) { alert(firstDDLValue); var grdPipeline = $find("#grid"); grdPipeline.set_dataSource(ReceiptList); grdPipeline._pi.show(grdPipeline); grdPipeline.applyClientBinding(); grdPipeline._pi.hide(grdPipeline); alert(firstDDLValue); $("#ItemName").text(Data); }); }); }); &lt;/script&gt; </code></pre> <p>Control:</p> <pre><code>public JsonResult SelectCustomerByID(string secondValue) { Receipt Recepit = new Receipt(); int CustID = 0; if (secondValue != "") CustID = Convert.ToInt32(secondValue); ObservableCollection&lt;Receipt&gt; ReceiptList = new ObservableCollection&lt;Receipt&gt;(); ReceiptList = Recepit.GetReceiptListByCustID(CustID); return Json(ReceiptList, JsonRequestBehavior.AllowGet); } </code></pre> <p>Model:</p> <pre><code>public ObservableCollection&lt;Receipt&gt; GetReceiptListByCustID(int CustID) { ObservableCollection&lt;Receipt&gt; ReceiptList = new ObservableCollection&lt;Receipt&gt;(); DataTable dtReceipt = new DataTable(); DbParameter[] objParams = new DbParameter[1]; objParams[0] = objDAL.CreateParameter("@REC_Cust_ID", DbType.Int32, CustID); dtReceipt = objDAL.ExecuteTable(CommandType.StoredProcedure, "[sp_Receipt_Select_ByCustID]",objParams); foreach (DataRow dr in dtReceipt.Rows) { ReceiptList.Add(new Receipt { Id = Convert.ToInt32(dr["REC_Id_I"]), Cust_Name = dr["CUS_Name_V"].ToString(), Pay_Amount = dr["REC_PaidAmount_M"].ToString(), Pay_Mode = dr["REC_PayMode_C"].ToString(), Bank_Name = dr["REC_BankName_V"].ToString(), Bank_Address = dr["REC_BankAddress"].ToString(), ChequeNo = dr["REC_ChequeNo_V"].ToString(), Cheque_Date = dr["REC_ChequeDate_D"].ToString(), Date = Convert.ToDateTime(dr["REC_Date_D"].ToString()), }); } return ReceiptList; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload