C#使用带枚举的If语句

时间:2016-04-18 12:49:40

标签: c# asp.net-mvc entity-framework enums code-first

我想显示一个不同的viewbag消息,具体取决于从枚举器的下拉列表中选择哪个选项但是我不知道如何执行此操作。我使用viewmodel并通过控制器传递数据,这里是ViewModel和类,关于如何使用ViewModel / Enumerators的if语句的解释将是非常有益的。我已经尝试过分配变量,但我意识到所有我正在做的就是说这就是这个,所以这不会从Orders Postage枚举中获取数据。任何帮助和解释都会受到赞赏,仍在学习。

视图模型;

{
    public class MyOrdersViewModel
    {
        public int OrderId { get; set; }
        public System.DateTime OrderDate { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }
        public decimal Total { get; set; }
        public bool HasBeenShipped { get; set; }
        public Postage? PostageList { get; set; }
        public List<MyOrderDetails> Details { get; set; }
    }

    public class MyOrderDetails
    {
        public string Title { get; set; }
        public string Colour { get; set; }
        public int Quantity { get; set; }
        public decimal UnitPrice { get; set; }
    }
}

控制器;

        public ActionResult Index(Order order)
        {
            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)
                    .OrderByDescending(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,
                        PostageList = o.PostageList,
                        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.PostageStatus
            //ViewBag.ShippedMessage = list.Where(w => w.HasBeenShipped).Any() ? "Order has been shipped" : "Order is being processed";

            Postage value = Postage.FirstClass;
            Postage value2 = Postage.StandardDelivery;
            Postage value3 = Postage.TwentyFourHour;

            if (value == Postage.FirstClass)
            {
                ViewBag.PostageStatus = ("First Class");
            }
            else
            {
                ViewBag.PostageStatus = (" Error ");
            }
            if (value2 == Postage.StandardDelivery)
            {
                ViewBag.PostageStatus = ("Standard Delivery");
            }
            if (value3 == Postage.StandardDelivery)
            {
                ViewBag.PostageStatus = ("24 hour delivery");
            }

return View(list);
        }

订单类

namespace T_shirt_Company_v3.Models
{
    //[Bind(Exclude = "OrderId")]
    public partial class Order
    {
        [ScaffoldColumn(false)]
        public int OrderId { get; set; }

        [ScaffoldColumn(false)]
        public System.DateTime OrderDate { get; set; }

        [ScaffoldColumn(false)]
        [Remote("CheckUserName", "Account")]
        public string Username { get; set; }

        [Required]
        [StringLength(16, ErrorMessage = "Your name is too long")]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Your last name is required.")]
        [StringLength(16, ErrorMessage = "Last name is too long.")]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Address is required.")]
        public string Address { get; set; }

        [Required(ErrorMessage = "City is required.")]
        public string City { get; set; }

        [Required(ErrorMessage = "Postcode is required.")]
        [Display(Name = "Post Code")]
        public string PostalCode { get; set; }

        [Required(ErrorMessage = "Country is required.")]
        public string Country { get; set; }

        [Required(ErrorMessage = "Phone number is required.")]
        public string Phone { get; set; }

        [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage = "Email doesn't look like a valid email address.")]
        public string Email { get; set; }

        [System.ComponentModel.DataAnnotations.Compare("Email")]
        [Display(Name = "Confirm your email address")]
        public string EmailConfirm { get; set; }


        [ScaffoldColumn(false)]
        public string PaymentTransactionId { get; set; }


        [ScaffoldColumn(false)]
        public bool HasBeenShipped { get; set; }

        [ScaffoldColumn(false)]
        //[ReadOnly(true)]
        public decimal Total { get; set; }

        [Required]
        [Range(0, 2, ErrorMessage = "Select a delivery method")]
        public Postage? PostageList { get; set; }

        public CardDetails cardDetails { get; set; }
        //public List<CardDetails> cardDetails { get; set; }
        public List<OrderDetail> OrderDetails { get; set; }
    }

    public enum Postage {[Display(Name = "Standard Delivery - Free")]StandardDelivery, [Display(Name = "First Class Delivery - £4.99")]FirstClass, [Display(Name = "24 Hour Delivery - £9.99")]TwentyFourHour }
}

1 个答案:

答案 0 :(得分:0)

为什么不在switch上使用enum语句?也许误解是这种类型是可以为空的。使用nullables,您可以在返回.GetValueOrDefault值时调用switch然后enum

switch (order.Postage.GetValueOrDefault())
{
    case Postage.StandardDelivery:
        ViewBag.PostageStatus = "Standard Delivery";
        break;

    case Postage.FirstClass:
        ViewBag.PostageStatus = "First Class";
        break;

    case Postage.TwentyFourHour:
        ViewBag.PostageStatus = "24 hour delivery";
        break;

    default:
        ViewBag.PostageStatus = "Error";
        break;
}

这可以防止使用if语句并简化用户的可读性。