Asp.net mvc 如何在局部视图MVC5上显示一个Id的结果
我正在尝试显示当前登录餐厅的预订,但出现以下错误 传递到字典的模型项的类型为“System.Collections.Generic.ListAsp.net mvc 如何在局部视图MVC5上显示一个Id的结果,asp.net-mvc,entity-framework,linq,Asp.net Mvc,Entity Framework,Linq,我正在尝试显示当前登录餐厅的预订,但出现以下错误 传递到字典的模型项的类型为“System.Collections.Generic.List1[RestaurantApplication.Models.Restaurant]”,但此字典需要类型为“System.Collections.Generic.IEnumerable1[RestaurantApplication.Models.RestaurantReservationEvent]”的模型项 这是我的控制器 public ActionRes
1[RestaurantApplication.Models.Restaurant]”,但此字典需要类型为“System.Collections.Generic.IEnumerable
1[RestaurantApplication.Models.RestaurantReservationEvent]”的模型项
这是我的控制器
public ActionResult RestaurantReservationsPartialView()
{
string currentUserId = User.Identity.GetUserId();
var restaurantID = (from r in db.Restaurants
where r.OwnerId == currentUserId
select r).ToList();
ViewBag.RestaurantId = restaurantID;
return PartialView("_RestaurantReservations", restaurantID);
这就是所谓的局部视图
<div style="padding-top: 50px;">
@Html.Action("RestaurantReservationsPartialView")
</div>
@Html.Action(“RestaurantReservationsPartialView”)
这是我想显示当前登录餐厅预订列表的部分视图
@if (ViewBag.numReservations > 0 && ViewBag.ReservationStatus == "Pending")
{
<center><p id="pendingReservations">Here are your pending reservations</p>
</center>
<div class="row">
@foreach (var item in Model)
{
<div class="col-md-4 col-sm-4 col-xs-6">
<div class="panel panel-primary">
<div class="panel-heading">
<center><h1 class="panel-title">@Html.DisplayFor(modelItem => item.BookersName)</h1></center>
</div>
<div class="panel-body">
<h3 style="font-size: 16px">Booking Description: @Html.DisplayFor(modelItem => item.BookingDesc) <br />
Number of people: @Html.DisplayFor(modelItem => item.BookingNumberOfPeople)</h3>
<h3 style="font-size: 15px">
<h>Booking Date: @Html.DisplayFor(modelItem => item.BookingDate) at</h>
<h>@Html.DisplayFor(modelItem => item.BookingStartTime)</h>
</h3>
</div>
<div class="panel-footer">
<center>@Html.ActionLink("Accept", "AcceptReservation", new { id = item.RestaurantReservationEventID }, new { @class = "btn btn-success btn-xs" }) @Html.ActionLink("Decline", "DeclineReservation", new { id = item.RestaurantReservationEventID }, new { @class = "btn btn-danger btn-xs" })</center>
</div>
</div>
</div>
}
@if(ViewBag.numReservations>0&&ViewBag.ReservationStatus==“待定”)
{
这是您的待定预订
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.BookerName)
预订说明:@Html.DisplayFor(modeleItem=>item.BookingDesc)
人数:@Html.DisplayFor(modelItem=>item.BookingNumberOfPeople)
预订日期:@Html.DisplayFor(modelItem=>item.BookingDate)位于
@DisplayFor(modelItem=>item.BookingStartTime)
@ActionLink(“接受”,“接受保留”,新{id=item.RestaurantReservationEventID},新{@class=“btn btn success btn xs”})@Html.ActionLink(“拒绝”,“取消保留”,新{id=item.RestaurantReservationEventID},新{@class=“btn btn danger btn xs”})
}
}首先,您不是从主视图调用部分视图,而是使用返回部分视图结果的
Html.action
方法调用操作方法
您的RestaurantReservationsPartialView
操作方法正在将Restaurant
对象列表传递给它的(部分)视图。但是从错误消息来看,您的部分视图似乎强类型化为RestaurantReservationEvent
对象列表。由于将错误的类型传递给局部视图,因此会出现此模型类型不匹配错误
解决方法是传递正确的类型。由于您希望传递特定餐厅的预订,您可能希望接受餐厅id作为操作方法的参数,使用该id获取预订事件并将其传递给视图
像这样的
public ActionResult RestaurantReservationsPartialView(int id)
{
var events = db.RestaurantReservationEvents.Where(x=>x.RestaurantId==id).ToList();
return View(events);
}
假设RestaurantReservationEvents
是DbContext上类型为DbSet
的属性。请更新linq表达式,该表达式返回与您的情况相匹配的餐厅列表
现在,确保在调用action方法时传递了餐厅id
<div style="padding-top: 50px;">
@Html.Action("RestaurantReservationsPartialView",,new { id=3})
</div>
@Action(“RestaurantReservationsPartialView”,,new{id=3})
在这里,我将Id值硬编码为
3
。您可以将其替换为具有实际值的变量。您需要将RestaurantReservationEvent
的集合传递给部分视图。而且,这不是局部视图调用。它正在执行一个操作方法Thank@Shyju,但是我只需要一个id(RestaurantID),我该怎么做呢?你有一个名为RestaurantReservationsPartialView
的操作方法吗?它看起来怎么样?是的,它在我帖子的顶部,控制器我没有看到它。请确保您在问题中发布了代码的相关部分。谢谢。此代码工作正常,但我在视图中获取RestaurantId时遇到问题,因为我使用@Model.IEnumerable?对于集合中的每个餐厅,您可能都有一个For/foreach循环。对吗?使用循环变量的相关属性(例如:RestaurantId/Id)是的,我在为每个循环使用一个,但是在部分视图进入该循环之前正在调用它