Dynamic Data Table Using Razor View
In this article we will get dynamic datable using razor view in asp.net mvc.
- Controller Action
- Strongly Type Razor View
Controller Action
public ActionResult PostedPYdetail(int EmpId) { try { if (EmpId > 0) { SqlParameter[] parameter = { new SqlParameter("pageIndex", System.Data.SqlDbType.Int) { Value = 1 } , new SqlParameter("pageSize", System.Data.SqlDbType.Int) { Value = 1000 } , new SqlParameter("EmployeeID", System.Data.SqlDbType.Int) { Value = EmpId } }; DataSet result = GetDataSet("store Procedure", parameter); return PartialView("~/Views/Shared/PartialViews/_pv.cshtml", result.Tables[0]); } else { return PartialView("~/Views/Shared/PartialViews/_pv.cshtml", new DataTable()); } } catch (Exception ex) { throw ex; } }
Partial View
@model System.Data.DataTable @using System.Data; <div class="form-group m-form__group row"> <label class="col-lg-2 col-form-label">Grand Total salary:</label> <div class="col-md-8"> <input class="GrandSumSalary form-control m-input form-control-sm" /> </div> </div> <div class="form-group m-form__group row"> <table class="table table-striped- table-bordered table-hover table-checkable" id="tblAddition"> <thead class="bg-secondary"> <tr> @foreach (DataColumn col in Model.Columns) { <th class="font-weight-bold @col.ColumnName">@col.ColumnName</th> } </tr> </thead> <tbody> @foreach (DataRow row in Model.Rows) { <tr> @foreach (DataColumn col in Model.Columns) { if (@row[col.ColumnName] == DBNull.Value) { <td>0</td>} else { <td>@row[col.ColumnName]</td>} } </tr> } </tbody> </table> </div>
0 Comments