I'm working on a ASP.NET MVC (an older version) application and I need to create a form to find reservations based on two parameters: Date and RoomID. There are underlying tables in the database: ReservationRequests and Rooms linked by RoomID.
I wrote this query in the Repository:
public IQueryable<ReservationRequest> FindReservationRequestInfo(DateTime date, int roomID)
{
return from requests in FindAllReservationRequests()
where requests.Date == date
& requests.RoomID == roomID
select requests;
}
The Model looks like:
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;
}
}
}
And the code in the Controller is:
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)));
}
}
} //
I'm not sure what is wrong with my code in the Controller. I will be very grateful if somebody could help me to understand what I did wrong. Thank you very much in advance!
Your query is returning
IQueryable<ReservationRequest>
but in your controller you're setting it equal to a variable that you declare as
IQueryable<string>
so this bit
[AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrator"), ValidateAntiForgeryToken]
public ActionResult FindReservationRequestInfo(FormCollection collection)
{
IQueryable<string> reservationRequestsInfo;
//...
should be
[AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrator"), ValidateAntiForgeryToken]
public ActionResult FindReservationRequestInfo(FormCollection collection)
{
IQueryable<ReservationRequest> reservationRequestsInfo;
//...
and then refactor appropriately for cascading effects of the change.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.