MVC+EF6教程三:Html Helpers和分部视图(Partial View)

上篇文章介绍了EF实现CRUD及一些基本的Html Helpers.

这次我们将会分类介绍Html Helpers和分部视图(Partial View)

1.分类介绍Html Helpers

2.分部视图(Partial View)

理论基础 -- Html Helpers

主要分成输入类和显示类。

输入类:

TextArea, TextBox

Password

Hidden

DropDownList

ListBox (与DropDownList类似,生存可多选的下拉列表框)

RadioButton

CheckBox

显示类:

显示类 Helper可以在应用程序中生成指向其他资源的链接,也可以构建被称为部分视图的可重用UI片段。

ActionLink和RouteLink

URL (Url.Action, Url.Content)

Partial 和 RenderPartial

Action和RenderAction

有两个helper说明一下:

html.ActionLink生成一个<a href=".."></a>标记

Url.Action只返回一个url。
例如:
@Html.ActionLink("linkText","someaction","somecontroller",new { id = "123" },null)
生成结果:

<a href="/somecontroller/someaction/123">linkText</a>

@Url.Action( "someaction", "somecontroller", new { id = "123" }, null)
生成结果:
/somecontroller/someaction/123

这些helper的特征是名称后面加上了 For , 这些叫做强类型的辅助方法。

对于不适合使用字符串字面值从View数据中提取值的情况,可以使用强类型辅助方法, 通过传递一个lambda表达式来指定要渲染的模型属性。表达式的模型类型必须和为View指定的强类型一致。

主要的强类型辅助方法。

Html.TextBoxFor();

Html.TextAreaFor();

Html.DropDownListFor();

Html.CheckboxFor();

Html.RadioButtonFor();

Html.ListBoxFor();

Html.PasswordFor();

Html.HiddenFor();

Html.LabelFor();

Html.EditorFor();

Html.DisplayFor();

Html.DisplayTextFor();

Html.ValidtionMessageFor()

模板页

Views文件夹下的Shared文件夹主要用来放共用的模板文件。创建MVC项目时会自动在Shared文件夹下生成一个_Layout.cshtml,如下图:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - 我的 ASP.NET 应用程序</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("应用程序名称", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("主页", "Index", "Home")</li>
                    <li>@Html.ActionLink("关于", "About", "Home")</li>
                    <li>@Html.ActionLink("联系方式", "Contact", "Home")</li>
                </ul>
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - 我的 ASP.NET 应用程序</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

@RenderBody():使用这个布局的View将把他们的内容显示到此处。显示效果:

要使用这个布局时,在View中添加 Layout="~Views/Shared/_Layout.cshtml"

@{
    Layout = "~Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Index";
}

Views文件夹下面有一个_ViewStart.cshtml,将这部分统一写到这个文件里(如下图),这样应用布局页的View就可以省略这部分内容了。 另外这个_ViewStart.cshtml也是可以嵌套的,使用布局页的View会自动应用最近文件夹下面的_ViewStart.cshtml。

理论基础 --分部视图(Partial View)

Partial View指可以应用于View中以作为其中一部分的View的片段(类似于之前的user control), 可以像类一样,编写一次, 然后在其他View中被反复使用。

一般放在"Views/Shared"文件夹中以共享。

创建Partial View:一般直接右键"Views/Shared"文件夹添加分部视图。

使用Partial View有两类helper :

Html.Partial / Html.RenderPartial

Html.Action / Html.RenderAction

猜你喜欢

转载自www.cnblogs.com/qianj/p/12499360.html