我有一个带有" HasBeenShipped"的ViewModel;这是一个布尔值,如何在我的控制器中将它发送到视图;
如果(hasbeenshipped = false)
我想发送给视图的消息 其他 订单已发货
在视图中我将如何显示其中一个? 可能有一个我认为的背包?
这是我的控制器
public ActionResult Index()
{
string currentUser = this.User.Identity.GetUserName();
List<T_shirt_Company_v3.ViewModels.MyOrdersViewModel> list = (from o in new TshirtStoreDB().Orders
.Where(o => o.Username == currentUser)
.OrderBy(o => o.OrderDate)
.Select(o => new MyOrdersViewModel()
{
OrderId = o.OrderId,
Address = o.Address,
FirstName = o.FirstName,
LastName = o.LastName,
City = o.City,
OrderDate = o.OrderDate,
PostalCode = o.PostalCode,
Total = o.Total,
HasBeenShipped = o.HasBeenShipped,
Details = (from d in o.OrderDetails
select new MyOrderDetails
{
Colour = d.Product.Colour,
Quantity = d.Quantity,
Title = d.Product.Title,
UnitPrice = d.UnitPrice
}).ToList()
}).ToList()select o).ToList();
//ViewBag.ShippedMessage = HasBeenShipped ? "blah" : "not shipped";
return View(list);
答案 0 :(得分:3)
因为ViewModel包含HasBeenShipped
,所以这应该有效:
<强> View.cshtml 强>
@model List<T_shirt_Company_v3.ViewModels.MyOrdersViewModel>
@foreach (var item in Model)
{
<span>@(item.HasBeenShipped ? "..." : "...")</span>
}
不需要使用ViewBag,或者只有在必须从外部源加载消息本身时才需要使用ViewBag。
答案 1 :(得分:1)
在您的控制器中使用ViewBag。如
ViewBag.ShippedMessage = list.Where(w=>w.HasBeenShipped).Any() ? "blah" : "not shipped";
然后在您的视图中,您可以访问该属性
<p>@ViewBag.ShippedMessage</p>