Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a table called StaffAppointment that I want to display in a view MVC4 that looks like the table below.

HTML
Staff no	date booked	Time From	time to
1	        2013-07-01	      3          4
1	        2013-07-09	      2	         3
1	        2013-07-18	      5	         7

But when I query the DB:

C#
var model = from x in dba. StaffAppointments where x.StaffNo == 1 select x;
return View(model.ToList());
It's displaying a duplicate of the first row like the table below.
On View
XML
@model IEnumerable<MyApp.Models.StaffAppointment>

XML
Staff no    date booked Time From   time to
1           2013-07-01        3          4
1           2013-07-01        3          4
1           2013-07-01        3          4


Please help solve this problem.
This is my Model class
C#
public class StaffAppointment
    {
        public Int32 Staffno { get; set; }
        public DateTime DateBooked { get; set; }
        public Nullable<int> TimeTo { get; set; }
        public Nullable<int> TimeFrom { get; set; }
    }

This is my View:
HTML
@model IEnumerable<MyApp.Models.StaffAppointment>

@{
    ViewBag.Title = "AddAppointment";
}


<table>
    <tr>
        <th>
           Staff no
        </th>
        <th>
           DateBooked
        </th>
        <th>
            TimeFrom
        </th>
        <th>
           TimeTo
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item. Staffno)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateBooked)
        </td>
       
        <td>
            @Html.DisplayFor(modelItem => item.TimeFrom)
        </td>
         <td>
            @Html.DisplayFor(modelItem => item.TimeTo)
        </td>
        
    </tr>
}

</table>
Posted
Updated 18-Jun-13 23:46pm
v2
Comments
saguptamca 19-Jun-13 4:38am    
plz update your view code
AMADIAR 19-Jun-13 5:55am    
This is my model class
public class StaffAppointment
{
public Int32 StaffNo { get; set; }
public DateTime DateBooked { get; set; }
public Nullable<int> TimeTo { get; set; }
public Nullable<int> TimeFrom { get; set; }
}
and My View
@model IEnumerable<myapp.models.staffappointment>

@{
ViewBag.Title = "AddAppointment";
}


<table>
<tr>
<th>
Staff no
</th>
<th>
DateBooked
</th>
<th>
TimeFrom
</th>
<th>
TimeTo
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item. Staffno)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateBooked)
</td>

<td>
@Html.DisplayFor(modelItem => item.TimeFrom)
</td>
<td>
@Html.DisplayFor(modelItem => item.TimeTo)
</td>

</tr>
}

</table>
saguptamca 19-Jun-13 4:41am    
and if possible, show ur model class

Hi,

After seeing this, i've identified that you didn't have any primary key. Most often, ORM creates problem (its called problem of identity mapping, which we can discuss later). For now try to rewrite your query like:
SQL
var model = from x in dba. StaffAppointments 
where x.StaffNo == 1 
select x.Staffno, x.DateBooked, x.TimeFrom, x.TimeTo;

return View(model.ToList());


It should resolve problem...
 
Share this answer
 
Comments
AMADIAR 20-Jun-13 2:45am    
Hi saguptamca,
Thank you so much for your suggestion. I added a primary key and It worked fine.
I think its problem of your query, which retrieve value corresponding just
C#
var model = from x in dba. StaffAppointments where x.StaffNo == 1 select ALL;
return View(model.ToList());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900