Tuesday, 8 September 2015

Creating HTML table in MVC using ViewData or ViewBag in C#


Controller Code here 

       public PartialViewResult searchStation()
        {
            try
            {
                ViewData["station"] = null;
                station = new Station();
                this.TryUpdateModel(station);   //Maps the View values to the station class
               StationDB  stationdb = new StationDB();
                DataTable dtStation = stationdb.searchStation(station);
                List<Station> lstStation = new List<Station>();
                foreach (DataRow dr in dtStation.Rows)
                {
                    lstStation.Add(new Station {
                                                 stationCode    = (string)dr["StationCode"],
                                                 stationName    = (string)dr["StationName"],
                                             
                                                });
                }
                ViewData["station"] = lstStation;
                ViewData.Model = station;
            }
            catch (Exception ex)
            {
               errorlog(ex);
            }
            return PartialView("~/Station/YourUserControlName", station);
        }


This View Code 

I have put my list  value into this  ViewData["Test"]

<%
    if (ViewData["Test"] != null)
    { %>
<div class="Grid" style="height:300px;width:750px;overflow:scroll;border:1px solid #0ADA0A;">
    <table border="1" width="100%" class="GridTable" id="results">
        <tr class="GridHeading">
            <th style="width: 30%">
                Station Code
            </th>
            <th style="width: 30%">
                Station Name
            </th>
         
        </tr>
        <%   int i = 0;
             foreach (var station in (IEnumerable<Your Model Class Name>)ViewData["Test"])
             {
                 i++;
                 if (i % 2 == 0)
                 {%>
        <tr class="GridAlternateRow">
            <%}
                 else
                 { %>
            <tr class="GridRow">
                <%} %>
                <td>
                    <%= Html.Encode(station.stationCode)%></a>
                </td>
                <td>
                    <%= Html.Encode(station.stationName)%>
                </td>
             
            </tr>
            <% }
             if (i == 0)
             { %>
            <tr class="GridRow">
                <td colspan="4" style="color: red;">
                    No Records Found
                </td>
            </tr>
            <%} %>
    </table>
    <div id="pageNavPosition" class="pagination" align="center" style="width:90%;overflow:auto" />
    <%  if (i != 0)
        { %>
    <br />
    <span class="button" id="spnButton"><span>
        <input id="btnPrint" type="button" onclick="return printForm();" value="  Print  " /></span></span>
    <%} %>
</div>
<%}
%>

<script type="text/javascript"><!--
    try {
        var pager = new Pager('results', 1000);
        pager.init();
        pager.showPageNav('pager', 'pageNavPosition');
        pager.showPage(1);
    } catch (e) {
        // alert(e.message);
    }
//--></script>

No comments:

Post a Comment