I try to use this solution:
MVC3 HiddenFor with Enum. Input's value is enum key not value?
However it does not work in MVC5 Razor view. I get this error:
An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code
Additional information: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
How can I get enum value in hidden field instead of enum key?
Enum type:
public enum LineType : int
{
Account = 0,
Inventory = 1,
Service = 2
}
In Razor view I have:
@Html.HiddenFor(m => (int)m.LineTypeID)
LineTypeID is of type LineType.
You can do like
@Html.HiddenFor(m => m.LineTypeID, new {@Value = (int)Model.LineTypeID})
This overrides key value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With