好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

ASP.NET MVC 动态设置模板

ASP.NET MVC 动态设置模板

在页面中添加如下代码(Page_PreInit事件代码中动态设置模板,前提是在页面Action中将用户信息存到ViewData["USER"]了):

view source print ?

<script runat= "server" >

     //动态设置模板

     protected   void   Page_PreInit( object   sender, EventArgs e)

     {

         switch   (ViewData.Eval( "USER.roleid" ).ToString())

         {

             case   "1" :   this .Page.MasterPageFile =   "~/Views/Shared/SuperAdmin.Master" ;

                 break ;

             case   "2" :   this .Page.MasterPageFile =   "~/Views/Shared/Manager.Master" ;

                 break ;

             default :   this .Page.MasterPageFile =   "~/Views/Shared/Default.Master" ;

                 break ;

         }

     }

</script>

如下使用CASE的SQL如何用LINQ TO SQL 来实现呢?

select   updateagaintime=( case     when   updateagaintime   is   null   then   begintime    else   updateagaintime   end )

from   workplan

熟悉LINQ的很快会写出这样的语句:

IQueryable<workplan> result = from w   in   dataContext.workplan

                               select   new   workplan

                               {

                                   updateagaintime = w.updateagaintime ==   null   ? w.begintime : w.updateagaintime

                               };

在MVC项目对应页面的Action中这样使用结果集:

var viewData =   new   MyDataView

{

      workPlanPageDataView = result.ToList().ToPagedList(pageIndex, pageSize)

};

上面的用法报错“ 不允许在查询中显式构造实体类型 ”(其中ToPagedList方法参见链接: 参考>> “ASP.NET MVC 分页”)

换了老赵的方法: http://jeffz.blog.51cto.com/309226/62391

代码如下(LINQ中使用SQL): 

MyProjectDataContext dataContext =   new   MyProjectDataContext();

dataContext.Connection.Open();

 

SqlCommand command =   new   SqlCommand(

                     @"SELECT [workplanid],

                              [requirementid],

                              [statusname],

                              [endtime],

                              [sellerid],

                              [begintime],

                              [technicianid], 

                              [updateagaintime]=(CASE  WHEN [updateagaintime] IS NULL THEN [begintime] ELSE [updateagaintime] END),

                              [difftime], [assitantsellerid], [remark], [isdeleted], [createtime], [createby], [updateby], [groupid]"   +

                     @"FROM [workplan]

                     ORDER BY [updateagaintime] DESC" ,

                     (SqlConnection)dataContext.Connection);

 

using   (DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))

{

     result = dataContext.Translate<workplan>(reader).AsQueryable<workplan>();

}

结果集用的IQueryable<workplan>类型,之后又根据不同的条件对结果集进行了几次不同的查询,错误提示为“ 无法枚举查询结果多次 ”(先前的几个查询语句只查了一个字段是为了节省篇幅)。

将结果集改为List<workplan>类型,即ToList<workplan>()之后问题解决。

本系列共7篇文章,目前已经完成如下内容:

IT人的学习方法论-1,讨论学习的方向

IT人的学习方法论-2,讨论学习的误区

IT人的学习方法论-3,讨论学习的方法

IT人的学习方法论-4,讨论一些重要的能力

IT人的学习方法论-5,也谈IT的创新

IT人的学习方法论-6,IT专家成长的十个学习的建议(上)

IT人的学习方法论-7,IT专家成长的十个学习的建议(下)

----------------------------------------*/

本文出自 “ 喻勇的博客 ”

相关文章:

《致IT同仁》(李云)

《初入IT职场》(人月神话)

《工作经验到底是个什么东东?工作经验从哪里来?》(周公)

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于ASP.NET MVC 动态设置模板的详细内容...

  阅读:55次