1

我正在开发一个 ASP.NET MVC(旧版本)应用程序,我需要创建一个表单来根据两个参数查找预订:Date 和 RoomID。数据库中有基础表:通过 RoomID 链接的 ReservationRequests 和 Rooms。

我在存储库中写了这个查询:

public IQueryable<ReservationRequest> FindReservationRequestInfo(DateTime date, int roomID)
    {
        return from requests in FindAllReservationRequests()
                where requests.Date == date

                    & requests.RoomID == roomID

                select requests;
    }

模型看起来像:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace RoomReservations.Models
{
    public class FindReservationRequestInfo
{
    public IQueryable<string> ReservationRequestsInfo { get; set; }
    public DateTime Date { get; set; }

    public SelectList Rooms { get; set; }

    public FindReservationRequestInfo(IQueryable<string> reservationRequestsInfo, DateTime date, SelectList rooms)
    {
        ReservationRequestsInfo = reservationRequestsInfo;
        Date = date;
        Rooms = rooms;
    }
}

}

Controller中的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using RoomReservations.Models;
using RoomReservations.Helpers;
using Telerik.Web.Mvc;


namespace RoomReservations.Controllers
{
    public class ReportsController : Controller
{
    IRepository Repository;

    public ReportsController()
        : this(new Repository())
    {
    }

    public ReportsController(IRepository repository)
    {
        Repository = repository;
    }


    //
    // GET: /Reports/FindReservationRequestInfo
    [Authorize(Roles = "Administrator")]
    public ActionResult FindReservationRequestInfo()
    {
        IEnumerable<Room> rooms = Repository.FindAllRooms();
        rooms = rooms.Concat(new List<Room>() { new Room() }.AsEnumerable()).OrderBy(o => o.ID);

        return View(new FindReservationRequestInfo(
            new List<string>().AsQueryable(), DateTime.Today,
            new SelectList(rooms, "ID", "Name")));
    }

    //
    // POST: /Reports/FindReservationRequestInfo
    [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrator"), ValidateAntiForgeryToken]
    public ActionResult FindReservationRequestInfo(FormCollection collection)
    {
        IQueryable<string> reservationRequestsInfo;
        DateTime date = DateTime.Today;

        Room room = Repository.GetRoom(Convert.ToInt32(collection["ID"]));

        if (collection["Date.Date"] == String.Empty)
            ModelState.AddModelError("Date.Date", "Date is required");



        if (ModelState.Sum(s => s.Value.Errors.Count()) == 0)
        {
            date = DateTime.Parse(collection["Date.Date"]);

            if (room == null)
            {
                room = new Room();
                reservationRequestsInfo = Repository.FindReservationRequestInfo(date, room.ID);
            }

        }
        else
        {
            reservationRequestsInfo = new List<string>().AsQueryable();
        }

        IEnumerable<Room> rooms = Repository.FindAllRooms();
        rooms = rooms.Concat(new List<Room>() { new Room() }.AsEnumerable()).OrderBy(o => o.ID);

        return View(new FindReservationRequestInfo(
            reservationRequestsInfo, date,
            new SelectList(rooms, "ID", "Name", room.ID)));
    }
 }

} //

我不确定控制器中的代码有什么问题。如果有人能帮助我理解我做错了什么,我将不胜感激。非常感谢您!

4

1 回答 1

2

您的查询正在返回

IQueryable<ReservationRequest>

但是在您的控制器中,您将其设置为等于您声明为的变量

IQueryable<string>

所以这一点

    [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrator"), ValidateAntiForgeryToken]
    public ActionResult FindReservationRequestInfo(FormCollection collection)
    {
        IQueryable<string> reservationRequestsInfo;
        //...

应该

    [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrator"), ValidateAntiForgeryToken]
    public ActionResult FindReservationRequestInfo(FormCollection collection)
    {
        IQueryable<ReservationRequest> reservationRequestsInfo;
        //...

然后针对变化的级联效应进行适当的重构。

于 2013-03-08T19:41:50.387 回答