C# 枚举和文本框组合的MVC5部分或编辑器模板?

C# 枚举和文本框组合的MVC5部分或编辑器模板?,c#,asp.net-mvc,razor,enums,asp.net-mvc-5.2,C#,Asp.net Mvc,Razor,Enums,Asp.net Mvc 5.2,我有几个Enums的以下结构: public Enum Title { Unknown = 0, Mr = 1, Mrs = 2, ... Other = -1 } public Enum Title { Unknown = 0, Mr = 1, Mrs = 2, ... Other = -1 } public enum MaritialStatus { Unspecified = 0, Sing

我有几个
Enum
s的以下结构:

public Enum Title
{
    Unknown = 0,
    Mr = 1,
    Mrs = 2,
    ...
    Other = -1
}

public Enum Title
{
    Unknown = 0,
    Mr = 1,
    Mrs = 2,
    ...
    Other = -1
}

public enum MaritialStatus
{
    Unspecified = 0,
    Single = 1,
    Married = 2,
    Separated = 3,
    Divorced = 4,
    ...
    Other = -1
}
还有其他一些。所有这些映射到实体框架属性和相关的“其他”属性:

public PersonEnums.Title Title { get; set; }
public string TitleOther { get; set; }
public string GetTitle => Title == PersonEnums.Title.Other ? TitleOther : Title.ToString();
现在,对于视图,我不确定将它们组合成一个控件/部分/编辑器模板的最干净/最简单的方法是什么

我试过:

添加视图模型:

public class EnumWithOtherViewModel
{
    public Enum Enum { get; set; }
    public string OtherValue { get; set; }
}
部分

@Html.Partial("_EnumWithOther", new EnumWithOtherViewModel { Enum = Model.Title, OtherValue = Model.TitleOther })
_EnumWithOther.cshtml

我收到以下错误返回类型“System.Enum”不受支持。在部分中的
@Html.EnumDropDownListFor()
行上

显示模板

@Html.EditorFor(m => new EnumWithOtherViewModel { Enum = m.Title, OtherValue = m.TitleOther })
EnumWithOther.cshtml

@model EnumWithOtherViewModel
@LabelFor(m=>m,新的{@class=“controllabel col-md-2”})
@Html.enumdopdownlistfor(m=>m.Enum,新的{@class=“form control EnumWithOther”})
@Html.ValidationMessageFor(m=>m.Enum,“,new{@class=“text danger”})
@EditorFor(m=>m.OtherValue)
我发现以下错误模板只能用于字段访问、属性访问、一维数组索引或单参数自定义索引器表达式。调用
编辑器for

对于如何最好地将泛型
枚举
字符串
其他值一起放入某种模板中,我有点不知所措。什么是使某些东西可用于此设置的最佳方法


显示和隐藏“other”框的javascript很简单,它只是将所有内容正确映射回我一直使用的EF。

感谢@ngm让我走上了正确的道路。看起来最干净的方式是使用HTML助手

public static MvcHtmlString EnumWithOther<TModel, TEnum>(this HtmlHelper<TModel> html, TEnum e, string other)
{
    string r = string.Empty; // Setup the return holder
    string label = e.GetType().Name; // Get the name of the Enum for the ID's
    r += html.EnumDropDownListFor(m => e, new { @id = label, @Name = label, @class = "form-control EnumWithOther" }); // Setup the dropdown for the enum
    r += html.TextBoxFor(m => other, new { @id = $"{label}Other", @Name = $"{label}Other" }); //Setup the textbox for other
    return new MvcHtmlString(r);
}
该帮助将输出HTML的默认
id
name
属性覆盖为表单的正确值,以便在提交时回发并正确映射


此时,您仍然需要处理JavaScript部分以根据需要显示和隐藏“其他”文本框。

对于第二个选项,使用编辑器模板,您是否尝试过在前一行中使用EnumWithOtherViewModel更新EnumWithOtherViewModel,然后在lambda中使用该变量?很好的调用-刚刚尝试过,它确实可以编译,但是
EnumDropDownFor
现在只是一个文本框,其值为,ID不正确。。。所以我不认为这是正确的方法…不知道为什么你会得到一个枚举文本框。至于id不正确,如果您指的是呈现html中的id属性,那么最好在主模型上使用EnumWithOtherViewModel类型的属性,并在此基础上调用EditorFor,在视图中动态构建它。@ngm这更有意义!!当然,只需直接从模型调用编辑器即可。我会给你一个机会go@ngm因此,如果我将
EnumWithOtherViewModel
添加到我的
person
模型中,EF会为Enum/String组合创建一个查找表,因此我要寻找的路径也不完全相同。。。只是想制作我自己的HtmlHelper,它将同时使用枚举和字符串
@model EnumWithOtherViewModel
<div class="form-group">
    @Html.LabelFor(m => m, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EnumDropDownListFor(m => m.Enum, new { @class = "form-control EnumWithOther" })
        @Html.ValidationMessageFor(m => m.Enum, "", new { @class = "text-danger" })
        @Html.EditorFor(m => m.OtherValue)
    </div>
</div>
public static MvcHtmlString EnumWithOther<TModel, TEnum>(this HtmlHelper<TModel> html, TEnum e, string other)
{
    string r = string.Empty; // Setup the return holder
    string label = e.GetType().Name; // Get the name of the Enum for the ID's
    r += html.EnumDropDownListFor(m => e, new { @id = label, @Name = label, @class = "form-control EnumWithOther" }); // Setup the dropdown for the enum
    r += html.TextBoxFor(m => other, new { @id = $"{label}Other", @Name = $"{label}Other" }); //Setup the textbox for other
    return new MvcHtmlString(r);
}
@Html.EnumWithOther(Model.Title, Model.TitleOther)