Andre Queen
Andre Queen

Reputation: 223

C# passing boolean value as message

I have a ViewModel with "HasBeenShipped" which is a boolean, how in my controller would I send it to the view as;

If (hasbeenshipped = false)

my message i want to send to the view else Order has been shipped

And in the view how would I display one of these? Possibly with a viewbag i thought?

Here is my controller

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);

Upvotes: 0

Views: 415

Answers (2)

Peter B
Peter B

Reputation: 24280

Because the ViewModel contains HasBeenShipped this should work:

View.cshtml

@model List<T_shirt_Company_v3.ViewModels.MyOrdersViewModel>

@foreach (var item in Model)
{
    <span>@(item.HasBeenShipped ? "..." : "...")</span>
}

No need to use ViewBag, or perhaps only if the messages themselves have to be loaded from an outside source, which should then be done in the Controller.

Upvotes: 3

drew
drew

Reputation: 1312

In your controller use ViewBag. Such as

ViewBag.ShippedMessage = list.Where(w=>w.HasBeenShipped).Any() ? "blah" : "not shipped";

Then in your view you can access that property

<p>@ViewBag.ShippedMessage</p>

Upvotes: 1

Related Questions