0

I have created Linq-to-SQL for a view of SQL Server, now I'm trying to display record on view, it's done :) but

  1. problem is that it shows each name 26 times, why?
  2. How to put it in gridview kinda thing in MVC?

Controller:

namespace EmployeeAttendance_app.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Employee Attendance";
            var DataContext = new EmployeeAtdDataContext();
            var EmployeeAtd = from emp in DataContext.EmployeeAtds
                         select emp;
            return View(EmployeeAtd);
        }

       }

View:

<ul>
 @foreach (EmployeeAttendance_app.Models.EmployeeAtd emp in (IEnumerable<Object>)ViewData.Model) 
 {
     <li>@emp.EmplName</li>
 }
</ul>

Model contains Linq-to-SQL class named EmployeesAtd.

View has been created via this query:

SELECT        
    dbo.HrEmployee.EmplID, dbo.HrEmployee.EmplName, dbo.AtdRecord.RecDate, 
    dbo.AtdRecord.RecTime, dbo.HrDept.DeptName
FROM            
    dbo.HrDept 
RIGHT OUTER JOIN
    dbo.HrEmployee ON dbo.HrDept.DeptID = dbo.HrEmployee.DeptID 
LEFT OUTER JOIN
    dbo.AtdRecord ON dbo.HrEmployee.EmplID = dbo.AtdRecord.EmplID
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
James
  • 49
  • 2
  • 12
  • 1
    Isn't in your case `from emp in DataContext.EmployeeAtds select emp;` exactly as `DataContext.EmployeeAtds`? In view do you need to cast to `IEnumerable` (and then implicitly cast each item again to `EmployeeAtd`)? That said I would check what you have in `EmployeeAtd` table inside your database... – Adriano Repetti Jan 16 '14 at 11:30
  • sorry sir, i didn't get ur first line ? – James Jan 16 '14 at 11:35
  • To expand on Adriano's suggestion, if you strongly type your view you should be able to shorten your loop code to: @foreach (var emp in ViewData.Model) – SethMW Jan 16 '14 at 11:36
  • when i don't put 'object' in IEnumerable; it says that it needs a type to be casted, 1 overload. so i put object – James Jan 16 '14 at 11:36
  • @James as SethMW said, make your view strongly typed. No need for all those casts (and your code will be more clear). That said, IMO problem is in your `RIGHT OUTER JOIN` because an employee may appear more than once in the other tables. I'd suggest to write an ad-hoc query for that or...simply use `DataContext.EmployeeAtds.Distinct(new ComparerByName())` in your controller. Where `CompareByName` is an equality comparer that consider only `EmplName`. – Adriano Repetti Jan 16 '14 at 11:42
  • ok sir i did my best to correct query but couldn't, could u please make it correct ? the query not c# code etc, just my query so it would not display records several times – James Jan 16 '14 at 12:16
  • i intentionally displayed only name, there is more to come – James Jan 16 '14 at 12:16
  • @James use `Distinct()` to make records unique (by employee's name). See my previous comment. – Adriano Repetti Jan 16 '14 at 12:27

2 Answers2

0

Other than cleaning up the things Adriano pointed out, I don't see anything wrong with that. You said EmployeeAtds is a SQL View, right? I would guess you have some sort of join in there that is duplicating rows.

SethMW
  • 1,062
  • 1
  • 9
  • 10
0
  1. Try to move boxing before loop:

    var emps = (IEnumerable< Object >)ViewData.Model;
    
    @foreach(EmployeeAttendance_app.Models.EmployeeAtd emp in emps)
    
  2. To show data in GridView you have to use some javascript lib. grid controls for ASP.NET MVC?

Community
  • 1
  • 1
emilpytka
  • 763
  • 7
  • 19