开发者

problem with Razor syntax and trying to output a string from a function

I have the following code in a .cshtml file, but each <option> is returned with nothing in it. I have verified the GetDescription() is retur开发者_StackOverflowning the right string, so I must have a syntax problem in my Razor code. Can someone tell me what the problem is please?

            <select>
            @{
                Array enumValues = null;
                enumValues = Enum.GetValues(typeof(SearchOperatorString));
                foreach (var type in enumValues)
                { 
                <option>
                    @{((Enum)type).GetDescription();} </option>
                }
            }
        </select>


You're making a statement block, which calls GetDescription, but does nothing with it's result.

You want to use a code nugget instead, which prints an expression to the page:

 @((Enum)type).GetDescription()

Instead of doing this manually, you should call the DropDownList helper:

@Html.DropDownList("myName", 
    enumValues.Cast<SearchOperatorString>()
              .Select(s => new SelectListItem { Text = e })
)


Try this:

@foreach(var type in Enum.GetValues(typeof(SearchOperatorString))){
    <option>@((Enum)type).GetDescription()</option>
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜