My model has property
public enum CheckStatus
{
A = 1,
B = 2,
C = 3,
}
public CheckStatus Status { get; set; }
and inside razor view I want to switch this property like
@switch (Model.Status)
{
case 1:
default:
<div>Selected A</div>
break;
case 2:
<div>Selected B</div>
break;
case 3:
<div>Selected C</div>
break;
}
Cannot implicitly convert type 'int' to 'CheckStatus'. An explicit conversion exists (are you missing a cast?)
Your switch
statement parameter and the case Label
must be of the same datatype.
so cast your enum to int
like this
switch ((int)Model.Status)
{
case 2:
<div>Selected B</div>
break;
case 3:
<div>Selected C</div>
break;
default:
<div>Selected A</div>
break;
}
or use the CheckStatus
in your case statement as well
switch (Model.Status)
{
case CheckStatus.B:
<div>Selected B</div>
break;
case CheckStatus.C:
<div>Selected C</div>
break;
default:
<div>Selected A</div>
break;
}
I removed the first case as you are not doing anything in that case. Also put the default
case at the end which make things readable. You can also use the Case 1
and remove the default
(if you want)
try this
switch((int) Model.Status) { }
to reach your goal!
Appendix: Model.Status
would just return A, B etc, not the integer values behind.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.