将View模型中的枚举绑定到MVC

时间:2016-03-20 22:27:16

标签: c# asp.net-mvc enums

每当我尝试创建票证时,我的票证优先级也为低。我为Priority设置了一个下拉列表,但是Tickets上的优先级分配似乎不起作用。我怀疑它与我的Views \ Ticket \ Create.cshtml

有关

我需要做一些澄清,看看我是否正确地做到了这一点:

我是否需要两者

在我的模型中,我有一个名为Ticket的类,它具有枚举优先级

pip install --upgrade awscli

Configuration.cs(用于插入票证数据的代码段)

public enum Priority
{
    Low, Med, High
}
public class Ticket
{
  ....
}

在我的viewmodel TicketVM中,我也为优先级设置了枚举。 由于我通过以下方式配置我的门票:

var tickets = new List<Ticket>
{
    new Ticket {
        UserID = users.Single(s => s.LastName == "Wan").UserID,
        CategoryID = categories.Single(c => c.CategoryName == "Con-X" ).CategoryID,
        Issue = ("Con-X Login Error"),
        Priority = Priority.High
    },
    new Ticket {
        UserID = users.Single(s => s.LastName == "Wan").UserID,
        CategoryID = categories.Single(c => c.CategoryName == "Desktop" ).CategoryID,
        Issue = ("Can't remote access C0123"),
        Priority = Priority.Med
    },
};

错误:

  

无法隐式转换类型   'RecreationalServicesTicketingSystem.ViewModels.TicketVM.Priority'来   'RecreationalServicesTicketingSystem.Models.Priority'。一个明确的   存在转换(您是否错过了演员?)   RecreationalServicesTicketingSystem

视图\票务\ Create.cshtml

            Ticket ticket = new Ticket
            {
                UserID = (int)WebSecurity.CurrentUserId,
                Issue = model.Issue,
                IssuedTo = model.IssuedTo,
                CategoryID = model.CategoryID,
                Priority = model.priority <-- ERROR
            };

的ViewModels \ TicketVM.cs

    <div class="form-group">
        @Html.DropDownListFor(m => m.priority, Model.PriorityList, "Please select the priority") 
    </div>

TicketController.cs

public class TicketVM
{
    public int UserID { get; set; }
    [Required(ErrorMessage = "Please enter the description")]
    public string Issue { get; set; }
    [Display(Name = "Administrator")]
    [Required(ErrorMessage = "Please select the Administrator")]
    public int IssuedTo { get; set; }
    public IEnumerable<SelectListItem> AdministratorList { get; set; }
    public int CategoryID { get; set; }
    public IEnumerable<SelectListItem> CategoryList { get; set; }

    public enum Priority
    {
        LOW = 3,
        MED = 2,
        HIGH = 1,
    }

    public Priority priority { get; set; }
    public IEnumerable<SelectListItem> PriorityList
    {
        get
        {
            var priorityType = typeof(Priority);
            var priorityValues = Enum.GetValues(priorityType).Cast<Priority>();
            var converter = TypeDescriptor.GetConverter(priorityType);

            return
                from priorityValue in priorityValues
                select new SelectListItem
                {
                    Text = converter.ConvertToString(priorityValue),
                    Value = priorityValue.ToString(),
                };
        }
    }
}

3 个答案:

答案 0 :(得分:2)

enums模型和Ticket模型中的TicketVM不一样。

一个选项是在TicketVM中添加using语句,以便您使用enum中的RecreationalServicesTicketingSystem.Models(并删除enum中的TicketVM定义} class)

@using RecreationalServicesTicketingSystem.Models
public class TicketVM
{
    ....
    public Priority priority { get; set; } // uses the enum from the Models assembly
}

另一个选择是从一个投射到另一个。由于您需要在对象初始值设定项中使用它,因此在控制器中创建一个私有方法来转换值

private Models.Priority CastPriority(ViewModels.TicketVM.Priority value)
{
    Models.Priority priority = Models.Priority.Low;
    Enum.TryParse<Models.Priority>(value.ToString(), out priority);
    return priority;
}

并在对象初始值设定项

Ticket ticket = new Ticket
{
    ....
    CategoryID = model.CategoryID,
    Priority = CastPriority(model.priority)
};

附注:您可以简化生成IEnumerable<SelectListItem>

的代码
public IEnumerable<SelectListItem> PriorityList
{
    get
    {
        return Enum.GetNames(typeof(Proprity)).Select(e => new SelectListItem() { Text = e, Value = e });
    }
}

答案 1 :(得分:1)

ViewModel中的其他Priority枚举不需要。只需使用模型中的一个添加相应的使用参考。

但是,如果您仍需要将一个枚举值分配给另一个变量,请执行此操作Priority=(int)model.priority

请注意,您的Priority模型包含Low=0Med=1High=2,而视图模型LOW = 3MED = 2HIGH = 1

答案 2 :(得分:1)

您似乎有两种不同的枚举类型priorityTicketVM.Priority&amp; Models.Priority并且错误消息表明系统无法隐式转换这两种类型。

您只能定义一个枚举类型并在代码中的任何位置使用它,因此您不需要枚举转换