你好,我需要帮助 我正在创建我的第一个ASP MVC网页。 我创建了一个与数据库连接的登录和注册页面。
我想从登录到Bookings表的客户那里传递CustomerId 这样它只显示与该客户有关的预订。 Bookings表具有CustomerId作为外键。到目前为止,这是我所做的。
public class BookingController : Controller
{
// GET: Booking
public ActionResult Index(int customerId)
{
TravelExpertsEntities bookingdb = new TravelExpertsEntities();
List<Booking> bookings = bookingdb.Bookings.Where(book =>
book.CustomerId == customerId).ToList();
return View(bookings);
}
}
}
//This is from login Controller
public ActionResult Login(Customer reg)
{
if (ModelState.IsValid)
{
var details = (from userlist in db.Customers
where userlist.UserName == reg.UserName &&
userlist.Password == reg.Password
select new
{
userlist.CustomerId,
userlist.UserName
}).ToList();
if (details.FirstOrDefault() != null)
{
Session["CustomerId"] =
details.FirstOrDefault().CustomerId;
Session["Username"] = details.FirstOrDefault().UserName;
return RedirectToAction("Index", "Booking");
}
}
else
{
ModelState.AddModelError("", "Invalid UserName or Password");
}
return View(reg);
}
我能够提取所有预订,但我想与已登录的客户进行过滤。
答案 0 :(得分:1)
如下所示替换您的RedirectToAction,以将customerId作为参数传递
var CustomerIdparam=details.FirstOrDefault().CustomerId;
RedirectToAction("Index", "Booking", new{customerId=CustomerIdparam});