4
Answers

How to display records in Rows in ASP.NET MVC5

Dear all,
 
   Compliment of the season, I am prety new to ASP.NET MVC but when through some tutorials and i am able to get my fingers on it step by step.
 I have some issue with the displaying my records in rows  below is my code from the Index.cshtml
  1. @model System.Data.DataTable  
  2. @{  
  3.     ViewBag.Title = "Index";  
  4. }  
  5.   
  6. <table class="table table-bordered table-striped table-responsive">  
  7.     <tr>  
  8.         <th>First Name</th>  
  9.         <th>Last Name</th>  
  10.         <th>Other Name</th>  
  11.         <th>Email</th>  
  12.         <th>Address</th>  
  13.         <th>B Month</th>  
  14.         <th>Martal Status</th>  
  15.         <th>Member Status</th>  
  16.         <th>  
  17.             Edit  
  18.         </th>  
  19.         <th>  
  20.             Delete  
  21.         </th>  
  22.   
  23.          
  24.     </tr>  
  25.     <tr>  
  26.       
  27.         @for (int i = 0; i < Model.Rows.Count; i++)  
  28.         {  
  29.             <td>@Model.Rows[i][1]</td>  
  30.             <td>@Model.Rows[i][2]</td>  
  31.             <td>@Model.Rows[i][3]</td>  
  32.             <td>@Model.Rows[i][4]</td>  
  33.             <td>@Model.Rows[i][5]</td>  
  34.             <td>@Model.Rows[i][6]</td>  
  35.             <td>@Model.Rows[i][7]</td>  
  36.             <td>@Model.Rows[i][8]</td>  
  37.             <td><a class="btn btn-primary" href="@Url.Action("Edit", "Member", new {@id=@Model.Rows[i][0] })" >Edit</a></td>  
  38.             <td><a class="btn btn-danger" href="@Url.Action("Delete", "Member", new {@id=@Model.Rows[i][0] })">Delete</a></td>  
  39.         }  
  40.     </tr>  
  41.      
  42. </table>  
  43.   
  44. <a href="@Url.Action("Create","Member")" class="btn btn-sm btn-primary">Add Member</a>  
  45.   
  46. @*<button id="my-button" onclick="location.href='@Url.Action("YourActionName", "YourControllerName")'">Submit</button>*@  
 my Model code is as follows (I am not using Entity Frame work)
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace HIGHGROUNDMCV.Models  
  9. {  
  10.   public   class MembersModel  
  11.     {  
  12.         public int ID { getset; }  
  13.       [DisplayName("First Name")]  
  14.         public string FName { getset; }  
  15.       [DisplayName("Last Name")]  
  16.         public string  LName { getset; }  
  17.       [DisplayName("Other Name")]  
  18.         public string  OName { getset; }  
  19.         public string  Email { getset; }  
  20.         public string  Address { getset; }  
  21.       [DisplayName("Month of Birth")]  
  22.         public int MOB { getset; }  
  23.       [DisplayName("Marital Status")]  
  24.         public string  MStatus { getset; }  
  25.       [DisplayName("Member Status")]  
  26.         public string  MembStatus { getset; }  
  27.     }  
  28. }  
then the result i am getting is as follows 
 
 
 I want the records to show in rows all show in a single row, please what am i doing wrong.
 
thank you for your help 
 
 
Answers (4)
0
Amit Kumar

Amit Kumar

NA 3.5k 203.6k 7y
Hi,
 
Your problem is not complex it is very easy. You want to print the data row by row but in your screen shot data is printed in same for solve this problem use <tr> tag inside the for loop like. Keeping mind read my comments and use my below code
  1. <table class="table table-bordered table-striped table-responsive">  
  2. <tr>  
  3. <th>First Name</th>  
  4. <th>Last Name</th>  
  5. <th>Other Name</th>  
  6. <th>Email</th>  
  7. <th>Address</th>  
  8. <th>B Month</th>  
  9. <th>Martal Status</th>  
  10. <th>Member Status</th>  
  11. <th>  
  12. Edit  
  13. </th>  
  14. <th>  
  15. Delete  
  16. </th>  
  17. </tr>  
  18. --<tr>---Remove it from here and use it inside for loop  
  19. @for (int i = 0; i < Model.Rows.Count; i++)  
  20. {  
  21. <tr>--use here  
  22. <td>@Model.Rows[i][1]</td>  
  23. <td>@Model.Rows[i][2]</td>  
  24. <td>@Model.Rows[i][3]</td>  
  25. <td>@Model.Rows[i][4]</td>  
  26. <td>@Model.Rows[i][5]</td>  
  27. <td>@Model.Rows[i][6]</td>  
  28. <td>@Model.Rows[i][7]</td>  
  29. <td>@Model.Rows[i][8]</td>  
  30. <td><a class="btn btn-primary" href="@Url.Action("Edit", "Member", new {@id=@Model.Rows[i][0] })" >Edit</a></td>  
  31. <td><a class="btn btn-danger" href="@Url.Action("Delete", "Member", new {@id=@Model.Rows[i][0] })">Delete</a></td>  
  32. <tr>---use here  
  33. }  
  34. ----</tr> Remove it from here and use it inside for loop  
  35. </table>  
Accepted
1
Dharmraj Thakur

Dharmraj Thakur

NA 4.1k 61.7k 7y
Please move your <tr> inside loop like this...
 
 
  1. @foreach (DataRow item in Model.Rows)  
  2. {  
  3.     <tr>  
  4.         <td>@Convert.ToString(item[1])</td>  
  5.         <td>@Convert.ToString(item[2])</td>  
  6.         <td>@Convert.ToString(item[3])</td>  
  7.         <td>@Convert.ToString(item[4])</td>  
  8.         <td>@Convert.ToString(item[5])</td>  
  9.         <td>@Convert.ToString(item[6])</td>  
  10.         <td>@Convert.ToString(item[7])</td>  
  11.         <td>@Convert.ToString(item[8])</td>  
  12.         <td><a class="btn btn-primary" href="@Url.Action("Edit", "Member", new {@id=Convert.ToString(item[0]) })">Edit</a></td>  
  13.         <td><a class="btn btn-danger" href="@Url.Action("Delete", "Member", new {@id=Convert.ToString(item[3]) })">Delete</a></td>  
  14.     </tr>  

0
Amit Kumar

Amit Kumar

NA 3.5k 203.6k 7y
Thanks,
 
    I have give designed code . you have to set only color. Paste it in html file and run it and set the color by your choice.
 
 
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<table style="width:80%">
<tr style="height:60px; background-color:magenta">
<td colspan="2">Setup For Composite Rates</td>
</tr>
<p>
</p>
<tr>
<td>
Category :
</td>
<td>
<select>
<option>INTERNAL DOOR</option>
<option>EXTERNAL DOOR</option>
</select>
</td>
</tr>
<tr>
<td>
Sub Category :
</td>
<td>
<select>
<option>Write your text</option>
<option>Another Text</option>
</select>
</td>
</tr>
<tr style="height:20px"><td colspan="2"></td></tr>
<tr>
<td>Description :<input type="text" /></td>
<td>Unit : <input type="text" /> &nbsp;&nbsp;&nbsp; Price : <input type="text" /> </td>
</tr>
<p></p>
<tr><td><i><b>Batch file Upload :</b></i></td><td></td></tr>
<tr>
<td>
<input type="file" /></td>
<td></td>
<tr>
<td><input type="button" class="btn-danger" value="Upload BulkCopy"/></td>
</tr>
</table>
</div>
</body>
</html>
 
0
Abraham Olatubosun

Abraham Olatubosun

NA 318 17.4k 7y
Many Thanks Amit Kumar it works fine.
 
I want to ask how is it posible to design my user interface so that i can have some label and textbox  in one row of a table, just the way we design it on ASP.NET
 Some thing like the one below:
 
 Thank you once again