dhl:枚举的另一种用法:C#中字符串和枚举类型互转
前言:
enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri}; //这样就从1开始了。
枚举型的隐含类型为 int 型, 例如
int x=(int)Days.Sat ; 一点问题没有。
int a =1 ; Days Days=(Days)a; 也是一点问题没有。
据MSDN说枚举类型的隐含类型可以为除了char以外的任意数字类型……
//一个enum的例子
enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };
//转换的例子
Colors myColor = (Colors)Enum.Parse(typeof(Colors), "Yellow");
正文:
先定义枚举:
代码
public enum LessonSearchOrderType
{
/// <summary>
/// 更新时间
/// </summary>
Ordertime,
/// <summary>
/// 浏览人气
/// </summary>
Orderhot,
/// <summary>
/// 课时费
/// </summary>
Orderprice,
/// <summary>
/// 课程费
/// </summary>
Ordertotalprice
}
后台处理:
if (!string.IsNullOrEmpty(search.OrderCondition))
{
var orderType = (EnumFactory.LessonSearchOrderType)Enum.Parse(typeof(EnumFactory.LessonSearchOrderType), search.OrderCondition, true);
switch (orderType)
{
case EnumFactory.LessonSearchOrderType.Ordertime:
break;
case EnumFactory.LessonSearchOrderType.Orderhot:
break;
case EnumFactory.LessonSearchOrderType.Orderprice:
break;
case EnumFactory.LessonSearchOrderType.Ordertotalprice:
break;
}
}
前台:
//html
<div class="listlb_tsright">
排序方式: <span class="orderstyle" id="<%=EnumFactory.LessonSearchOrderType.Ordertime %>">
更新时间</span> <span class="orderstyle" id="<%=EnumFactory.LessonSearchOrderType.Orderhot %>">
浏览人气</span> <span class="orderstyle" id="<%=EnumFactory.LessonSearchOrderType.Orderprice %>">
课时费</span> <span class="orderstyle" id="<%=EnumFactory.LessonSearchOrderType.Ordertotalprice %>">
课程费</span>
</div>
//排序js:
<%foreach(string s in Enum.GetNames(typeof(EnumFactory.LessonSearchOrderType)))
{
%>
$("#<%=s %>").click(
function(){
setOrder("<%=s %>");
}
);
<%
}
%>
var setOrder = function(orderid)
{
window.location.href = "<%=Url.Action("Lesson", "Taoke")%>" + $.query.set("order",$("#"+orderid).attr("id"));
}
预览: