Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum value instead of key in hidden HiddenFor field in Razor view (Asp.Net MVC 5)

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.

like image 229
Arunas Avatar asked Feb 10 '23 00:02

Arunas


1 Answers

You can do like

@Html.HiddenFor(m => m.LineTypeID, new {@Value = (int)Model.LineTypeID})

This overrides key value

like image 124
Sergey Buglak Avatar answered Feb 12 '23 11:02

Sergey Buglak