gpt4 book ai didi

asp.net-mvc-4 - 多选枚举

转载 作者:行者123 更新时间:2023-12-02 19:47:48 30 4
gpt4 key购买 nike

我正在开发一种工具,以便我可以在游戏中保持井井有条。

这是我的课:

//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; }
}

Label/Type/TradeType/Trade 是枚举。

查看:

@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>

枚举有完整的可能性列表,我想创建一个项目创建 View

我遇到的问题:

我希望能够从枚举中选择更多选项。 (Like this)其中类别是我的枚举。

这在 asp.net mvc 4 中可能吗?

(小注:我还是一名学生,但这不是学校项目)

最佳答案

创建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>
}
}

您的枚举可能如下描述:

[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,
}

比,使用:

    <div class="editor-field">
@Html.LabelFor(m => m.Trade)
@Html.EditorFor(m => m.Trade, "Options")
</div>

关于asp.net-mvc-4 - 多选枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17514189/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com