LINQ存储库&方法返回

时间:2016-05-06 21:52:42

标签: c# asp.net-mvc linq repository-pattern

我是C#,MVC,LINQ,实体框架以及我正在做的其他事情的新手。我在这个问题上选择了stackoverflow脑信任和其他网站,但没有看到如何解决它。

我正在尝试为我们正在构建的新应用程序实现Repository模式,并且在返回查询结果时遇到问题。第一个也是最紧迫的问题是我收到下面的错误,第二个问题更多的是弄清楚如何处理查询中的空结果。

对于此问题,我试图从仪表板视图中显示数据库中的请求列表。我们有一个使用SQL查询的工作原型,我试图用存储库替换它,这可能是过度的,但这是我们认为我们想做的事情。

以下是观点:

@using RAM.DAL.Models
@model RequestViewModel
@{
    ViewBag.Title = "Dashboard";
} 

<h1>Dashboard</h1>

<div class="col-md-4">
    <h2>Resource Requests</h2>
        <div class="panel">
            <table class="table table-hover">
                <thead>
                    <tr class="bg-primary">
                        <th>Number</th>
                        <th>Mission Title</th>
                        <th>Resource Requested</th>
                        <th>Person</th>
                    </tr>
                </thead>
                <tbody>
                    @if (Model.isEmpty)
                    {
                        <tr>
                            <td colspan="4">No requests pending</td>
                        </tr>
                    }
                    else
                    {
                        <tr onclick="location.href= '@Url.Action("Assignment", "Mission", new { id = Model.ID })'">
                            <td>@Model.ID</td>
                            <td>@Model.title</td>
                            <td>@Model.resourceTitle</td>
                            <td>@Model.userName</td>
                        </tr>
                    }
                </tbody>
            </table>
    </div>
    <!--<p><a class="btn btn-default" href="#">Content 1 Btn &raquo;</a></p>-->
</div>

这是ViewModel:

using System;

namespace RAM.DAL.Models
{
    public class RequestViewModel
    {
        public int? ID { get; set; }
        public string title { get; set; }
        public string requestText { get; set; }
        public string user_ID { get; set; } //The userID of the user being requested.
        public string userName { get; set; } //Full name of the user being requested
        public int? fromResourceID { get; set; } //The resource where the request was generated from
        public int? toResourceID { get; set; } //The resource where the reassigned worker is requested to go to
        public string resourceTitle { get; set; } //Title of the resource where the reassigned worker is requested to go to
        public DateTime? requestDate { get; set; }//The date the request was made
        public bool? isEmpty { get; set; }
    }
}

这里是GetRequests方法的存储库,我遇到了问题(其余的还没有实现):

using RAM.DAL.Models;
using System;
using System.Linq;
using System.Data;
using System.Collections.Generic;

namespace RAM.DAL
{
    public class RequestRepository : IRequestRepository<RequestViewModel>
    {

        private RAMcontext context;

        public RequestRepository(RAMcontext context)
        {
            this.context = context;
        }

        public IEnumerable<RequestViewModel> GetRequests()
        {
            var requests = from r in context.RAM_Requests
                    join u in context.Users on r.user_ID equals u.User_ID
                    join res in context.RAM_Resources on r.toResourceID equals res.ID
                    where r.resolved == false
                    select new RequestViewModel()
                    {
                        title = r.title,
                        ID = r.ID,
                        fromResourceID = r.fromResourceID,
                        toResourceID = r.toResourceID,
                        user_ID = r.user_ID,
                        userName = u.First_Name + " " + u.Last_Name,
                        resourceTitle = res.title,
                        requestText = r.requestText,
                        requestDate = r.requestDate
                     };
            /*   }
              catch
              {

               RequestViewModel empty = new RequestViewModel
                  {
                      isEmpty = true
                  };
               return empty;
              }*/
            return requests.ToList().AsEnumerable();
        }

我得到的错误是:

  

传递到字典中的模型项是类型的   &#39; System.Collections.Generic.List`1 [RAM.DAL.Models.RequestViewModel]&#39 ;,   但是这个字典需要一个类型的模型项   &#39; RAM.DAL.Models.RequestViewModel&#39;

1 个答案:

答案 0 :(得分:0)

从错误消息中,我猜您的操作方法是向视图发送RequestViewModel的集合。但是您的视图强烈地键入RequestViewModel的单个实例,而不是集合。这就是你得到这个错误的原因。

由于您要显示请求集合,因此应将视图更改为强类型到集合。您可以使用LINQ Any()方法确定传递给视图的集合中是否有多个项目,并显示/隐藏消息/表格以显示数据。

@model IEnumerable<RequestViewModel>
<h1>Dashboard</h1>
@if(!Model.Any())
{
  <p>No records found </p>
}
else
{
  <table>
     <tr>
         <th>Title</th>
         <th>User name </th>
     </tr>
  @foreach(var req in Model)
  {
     <tr>
         <td>@req.title</td>
         <td>@req.userName</td>
     </tr>
  }
  </table>
}