Html.HiddenFor与枚举 [英] Html.HiddenFor with enum

查看:339
本文介绍了Html.HiddenFor与枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的看法如下:

@Html.HiddenFor(x => x.MeasurementUnitType)

MeasurementUnitType是一个枚举它看起来像:

MeasurementUnitType is an enum which looks like:

public enum MeasurementUnitType
{
    Metric, Imperial
}

不管发生什么事,以对模型枚举,隐藏字段总是被设置为公制。

Whatever happens to that enum on the model, the hidden field is always set to metric.

我也尝试有它的编辑模板:

I did try having an editor template for it:

@using Ns.Domain.Models.Enums
@model Ns.Domain.Models.Enums.MeasurementUnitType
@{
    switch (Model)
    {
        case MeasurementUnitType.Metric:
    <text>
    @Html.Hidden("Metric", ViewData.TemplateInfo.FormattedModelValue)
    </text>
break;
        case MeasurementUnitType.Imperial:
    <text>
    @Html.Hidden("Imperial", ViewData.TemplateInfo.FormattedModelValue)
    </text>
break;
        default:
throw new ArgumentOutOfRangeException();
    }
}

但将输出隐藏字段为

but that would output the hidden field as

 <input id="NewTable_MeasurementUnitType_Metric" name="NewTable.MeasurementUnitType.Metric" type="hidden" value="Metric" />

 <input id="NewTable_MeasurementUnitType_Imperial" name="NewTable.MeasurementUnitType.Metric" type="hidden" value="Imperial" />

分别

这不会工作作为ID具有枚举的实际价值,以及作为标识的枚举的名字......

this wont work as the Id has the actual value of the enum as well as the name of the enum in the Id...

任何人有什么想法?

推荐答案

这是现在支持开箱即用的mvc5

This is now supported out of the box in mvc5

的https://aspnet.$c$cplex.com / SourceControl /最新#样品/ MVC / EnumSample /

这篇关于Html.HiddenFor与枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆