Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi selectable enum

I am working on a tool so i can keep things organized in a game.

This is my class:

//The item
public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Value { get; set; }
    public ItemLabel Label { get; set; }
    public ItemType Type { get; set; }
    public ItemTradeType TradeType { get; set; }
    public Trade Trade { get; set; }
}

The Label / Type / TradeType / Trade are enums.

View:

@model EveMonitorV2.Models.Item

@{
    ViewBag.Title = "AddItem";
}

<h2>AddItem</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Item</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Value)
        </div>

//What should be done here?

        <div class="editor-field">
            @Html.EditorFor(model => model.Value)
            @Html.ValidationMessageFor(model => model.Value)
        </div>


        <div class="editor-label">
            @Html.LabelFor(model => model.Trade)
        </div>
        <div class="editor-field">
            @Html.CheckBoxFor()
            @Html.ValidationMessageFor(model => model.Trade)
        </div>


        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

The enum has a whole list of possibilities and I want to make an Item Create view

The problem i run into:

I want to be able to select more options from the enums. (Like this) Where the categories are my enums.

Is this possible at all in asp.net mvc 4?

(little note: I am still a student, but it isn't a school project)

like image 298
JochemQuery Avatar asked Jan 19 '26 10:01

JochemQuery


1 Answers

Create View\Shared\EditorTemplates\Options.cshtml

@using System.ComponentModel.DataAnnotations
@using System.Reflection
@model Enum
@{
    var name = ViewData.TemplateInfo.HtmlFieldPrefix;
    var type = Model.GetType();
}

@foreach (Enum e in Enum.GetValues(type))
{
    var display = type.GetField(e.ToString()).GetCustomAttribute<DisplayAttribute>();
    if (display != null && (display.GetAutoGenerateField() ?? true))
    {
    <label class="checkbox" title="@display.GetDescription()">
        <input type="checkbox" name="@name" value="@e.ToString()" checked="@Model.HasFlag(e)" />
        @display.Name
    </label>
    }
}

your enum may be described as next:

[Flags]
public enum MyOptions
{
    [Display(AutoGenerateField = false)]
    None = 0,
    [Display(Name = "Option 1 name")]
    Opt1 = 1 << 1,
    [Display(Name = "Option 2 name")]
    Opt2 = 1 << 2,
    [Display(Name = "Option 3 name")]
    Opt3 = 1 << 3,
}

than, using:

    <div class="editor-field">
        @Html.LabelFor(m => m.Trade)
        @Html.EditorFor(m => m.Trade, "Options")
    </div>
like image 180
hVostt Avatar answered Jan 21 '26 06:01

hVostt