1. 程式人生 > >知識點隨筆記錄二

知識點隨筆記錄二

區分 role HERE 使用 sde 檢測 helper 直接 reat

十二、linq關聯表查詢自己總結的幾種方式:
1、直接join(可以不使用導航屬性
2、var user1 = from b in db.sysUsers
from c in b.SysUserRoles
where b.ID == id
select b;
必須使用導航屬性
生成的SQL:
SELECT TOP (1)
[Extent1].[ID] AS [ID],
[Extent1].[UserName] AS [UserName],
[Extent1].[Email] AS [Email],
[Extent1].[PassWord] AS [PassWord],

[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[SysDepartmentID] AS [SysDepartmentID]
FROM [dbo].[SysUser] AS [Extent1]
INNER JOIN [dbo].[SysUserRole] AS [Extent2] ON [Extent1].[ID] = [Extent2].[SysUserID]
WHERE [Extent1].[ID] = 5

可以知道,是INNER JOIN。

3、Include方式(Eager loading)
var user = from a in db.sysUsers.Include("SysUserRoles").Include("SysDepartment")
where a.ID == id
select a;
必須使用導航屬性
生成的SQL:
SELECT
[Project1].[ID] AS [ID],
[Project1].[UserName] AS [UserName],
[Project1].[Email] AS [Email],
[Project1].[PassWord] AS [PassWord],
[Project1].[CreateDate] AS [CreateDate],
[Project1].[SysDepartmentID] AS [SysDepartmentID],
[Project1].[ID1] AS [ID1],
[Project1].[DepartmentName] AS [DepartmentName],
[Project1].[DepartmentDesc] AS [DepartmentDesc],
[Project1].[C1] AS [C1],
[Project1].[ID2] AS [ID2],
[Project1].[SysUserID] AS [SysUserID],
[Project1].[SysRoleID] AS [SysRoleID]
FROM ( SELECT
[Limit1].[ID] AS [ID],
[Limit1].[UserName] AS [UserName],
[Limit1].[Email] AS [Email],
[Limit1].[PassWord] AS [PassWord],
[Limit1].[CreateDate] AS [CreateDate],
[Limit1].[SysDepartmentID] AS [SysDepartmentID],
[Extent2].[ID] AS [ID1],
[Extent2].[DepartmentName] AS [DepartmentName],
[Extent2].[DepartmentDesc] AS [DepartmentDesc],
[Extent3].[ID] AS [ID2],
[Extent3].[SysUserID] AS [SysUserID],
[Extent3].[SysRoleID] AS [SysRoleID],
CASE WHEN ([Extent3].[ID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM (SELECT TOP (1) [Extent1].[ID] AS [ID], [Extent1].[UserName] AS [UserName], [Extent1].[Email] AS [Email], [Extent1].[PassWord] AS [PassWord], [Extent1].[CreateDate] AS [CreateDate], [Extent1].[SysDepartmentID] AS [SysDepartmentID]
FROM [dbo].[SysUser] AS [Extent1]
WHERE [Extent1].[ID] = 4 ) AS [Limit1]
LEFT OUTER JOIN [dbo].[SysDepartment] AS [Extent2] ON [Limit1].[SysDepartmentID] = [Extent2].[ID]
LEFT OUTER JOIN [dbo].[SysUserRole] AS [Extent3] ON [Limit1].[ID] = [Extent3].[SysUserID]
) AS [Project1]
ORDER BY [Project1].[ID] ASC, [Project1].[ID1] ASC, [Project1].[C1] ASC

是LEFT OUTER JOIN。

十三、輔助器
1,內聯輔助器,定義在視圖裏沒有返回值的方法,只在該頁面有效。以@helper定義。
2,外部輔助器,定義mvchtmlstring對象的擴展方法,內部定義html元素用TagBuilder類。
3,內建輔助器,包括表單輔助器、普通input輔助器、強類型input輔助器、普通select輔助器、強類型select輔助器、生成鏈接的輔助器等。

十四、分段
1、在視圖中使用@section定義分段內容,在布局中通過@RenderSection(分段名稱)使用。
2、通用的部分可以定義成分段,如header、footer,甚至body也可以,布局中使用@RenderSection("header")、@RenderSection("footer")、@RenderSection("body")來使用分段;
3、IsSectionDefined(分段名稱)檢測分段是否已定義;
4、通過@RenderSection(分段名稱,false)重載參數指定分段是否必須定義,如未定義也不會拋出異常。

十五、子動作
ChildActionOnly對於子動作不是必須的,加上是為了防止用戶直接調用;

十六、模板視圖輔助器(Templated View Helper)

不指定用什麽HTML元素顯示類型的屬性,而是讓MVC框架自己去判斷該用什麽樣的HTML元素。
1、顯示視圖(如Display、DisplayFor)和編輯視圖(如Edit、EditFor)
2、Display、DisplayFor、Edit、EditFor、Lable、LableFor、DisplayText、DisplayTextFor

3、支架(Scaffolding)
是為一個模型對象(而非針對單個屬性)的所有屬性生成HTML輔助器的套件。
DisplayForModel()
EditForModel()
LableForModel()

4、模板輔助器HTML輔助器的區分(自己總結)
模板輔助器不明確指定用哪個HTML元素顯示,讓MVC自己去判斷;
HTML輔助器包括表單輔助器、Input輔助器、select輔助器、生成url輔助器等,是明確指定了用什麽HTML控件來生成;
模板輔助器更加靈活;

5、總結:
模板(視圖)輔助器有兩大類,一類叫做模板輔助器,其作用就是在視圖中為模型的個別屬性創建顯示/編輯視圖;
另一類叫做支架模板視圖,其作用是在視圖中創建模型的所有屬性的顯示/編輯視圖。

知識點隨筆記錄二