显示重复行的剃刀视图 [英] Razor view displaying repeating rows

查看:78
本文介绍了显示重复行的剃刀视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个名为StaffAppointment的表,我想在一个MVC4视图中显示,如下表所示。



<前lang =HTML>工作人员没有预约日期时间从
1 2013-07-01 3 4
1 2013 -07-09 2 3
1 2013-07-18 5 7



但是当我查询数据库时:



  var  model = 来自 x  dba中的code-keyword> StaffAppointments 其中 x.StaffNo ==  1  选择 x; 
return 查看(model.ToList());

它显示第一行的副本,如下表所示。

On View

 @ model IEnumerable <   MyApp.Models.StaffAppointment  >  



员工没有预约日期时间从
1 2013-07-01 3 4
1 2013-07-01 3 4
1 2013-07-01 3 4





请帮忙解决这个问题。

这是我的Model类

  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 ; }
}



这是我的观点:

 @ model IEnumerable <   MyApp.Models.StaffAppointment  >  

@ {
ViewBag.Title =AddAppointment;
}


< table >
< tr < span class =code-keyword>>
< th >
员工没有
< / th >
< th >
DateBooked
< / th >
< th >
TimeFrom
< / th >
< th >
TimeTo
< / th >
< th > < / th >
< / tr >

@foreach(模型中的var项目){
< < span class =code-leadattribute> tr >
< td >
@ Html.DisplayFor(modelItem =>项目。 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 >

解决方案





看到这个后,我发现你没有任何主键。大多数情况下,ORM会产生问题(称为身份映射问题,我们稍后可以讨论)。现在尝试重写您的查询,例如:

 var model = 来自 x  in  dba。 StaffAppointments 
其中 x.StaffNo == 1
选择 x.Staffno,x.DateBooked,x.TimeFrom,x.TimeTo;

return 查看(model.ToList());





它应该解决问题...


我认为你的查询问题,检索值对应的只是

  var  model = 来自 x <在 dba中的span class =code-keyword>。 StaffAppointments 其中 x.StaffNo ==  1  选择全部; 
return 查看(model.ToList());


Hi,
I have a table called StaffAppointment that I want to display in a view MVC4 that looks like the table below.

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:

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

@model IEnumerable<MyApp.Models.StaffAppointment>


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

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:

@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>

解决方案

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:

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...


I think its problem of your query, which retrieve value corresponding just

var model = from x in dba. StaffAppointments where x.StaffNo == 1 select ALL;
return View(model.ToList());


这篇关于显示重复行的剃刀视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆