我已经为MVC Controller添加了一些代码以启用过滤功能。一切都很好,它没有识别数据库表,编译器告诉我名称' bookingDate'在当前上下文中不存在,但在上面几行中从数据库中提取数据它识别它很好,智能感知甚至建议自动完成它。
如果我对该行进行注释,那么第二个ViewBag就会出现错误,说明一个也不存在!下面是我的控制器的代码。我正在为我的ORM使用DB First Entity框架。我删除了其余的控制器代码,试图保持这个简短而甜蜜但我可以发布它,如果你需要它。
BookingController:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MooWoosCRM;
namespace MooWoosCRM.Controllers
{
public class BookingsController : Controller
{
private Entities db = new Entities();
// GET: Bookings
public ActionResult Index(string sortOrder)
{
ViewBag.bookingDate = (from r in db.bookings
select r.bookingDate).Distinct();
ViewBag.stallType = (from r in db.bookings
select r.stallType).Distinct();
var model = from r in db.bookings
where r.bookingDate == bookingDate || bookingDate == null || bookingDate == ""
where r.stallType == stallType || stallType == null || stallType == ""
select r;
ViewBag.stallSortParm = String.IsNullOrEmpty(sortOrder) ? "stall_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
var bookings = from s in db.bookings
select s;
switch (sortOrder)
{
case "stall_desc":
bookings = bookings.OrderByDescending(s => s.stallType);
break;
case "Date":
bookings = bookings.OrderBy(s => s.bookingDate);
break;
case "date_desc":
bookings = bookings.OrderByDescending(s => s.bookingDate);
break;
default:
bookings = bookings.OrderBy(s => s.bookingTime);
break;
}
return View(db.bookings.ToList());
}
// GET: Bookings/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
booking booking = db.bookings.Find(id);
if (booking == null)
{
return HttpNotFound();
}
return View(booking);
}