整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

ASP.NET MVC知识盘点:视图

ASP.NET MVC知识盘点:视图

Razon语法

使用@符号后接C#或VB.NET语句的方式。

基本规则

1)变量

@后直接变量即可

2)代码块

为使用表达式或多行代码,@后跟大括号将多行代码包括在大括号中

3)“+”

对于加号连接的两个字符串变量或属性,使用小括号将他们括起来

4)插入HTML或文字

每一行前面加上“@:”

5)使用注释

使用@*和*@将要注释的部分包起来

6)用@@在页面上显示@

@using

在一个View中引入此页所需程序集的命名空间。

还可以在web.config中配置命名空间,不过将对所有的View起作用。

<system.web.webPages.razor>
 <pages pageBaseType="System.Web.Mvc.WebViewPage">
 <namespaces >
 <add namespace="System.Web.Mvc"/>
 <add namespace="WebApplication.Models"/>
 </namespaces>
 </pages>
</system.web.webPages.razor>

@model

指定页面所用模型的类型。

@help

使用自定义函数。这种方法有可能将一部分数据处理逻辑放到了页面中,所以尽量不用。

例子:

定义函数

@helper CheckHelp(int i1,int i2)
 {
 if (i1 > i2)
 {
 @i1
 }
 else
 {
 @i2 
 }
	}

使用函数

<h3>@CheckHelp(10,1111)</h3>

@functions

定义一个方法供当前页使用,若使用IHtmlString作为方法的返回值,则可将其回传给当前页。

例子:

定义函数

@functions 
 {
 public int CheckFunc(int i1, int i2)
 {
 if (i1 > i2)
 {
 return i1;
 }
 else
 {
 return i2;
 }
 }
	}

使用函数

<h3>@CheckFunc(10, 12111)</h3>

2 HTML辅助方法

使用方式为@后跟辅助方法,注意没有“;”,否则分号也会显示在页面上。

2.1输出超链接

ActionLink

有几个重载方法,选参数最多的一个

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes);

linkText:超连接名称

actionName:操作名称

controllerName:控制器名称

protocol:URL 协议,如“http”或“https”。

hostName:URL 的主机名

fragment:URL 片段名称(定位点名称)

routeValues:路由参数

htmlAttributes:HTML 特性

例:

@Html.ActionLink("一个连接", "About")

对应的html代码

<a href="/MVCPointApp/Home/About">一个连接</a>

RouteLink

有几个重载方法,选参数最多的一个

public static MvcHtmlString RouteLink(this HtmlHelper htmlHelper, string linkText, string routeName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes);

routeName:路由名称

其他参数同ActionLink

2.2输出表单

还可以使用强类型的辅助方法,一般是以For结尾。

例子

1)显示属性验证信息

控制器

public ActionResult TestViewData(ModelF mf)
 {
 ViewData.Model=new ModelF { Field=mf.Field, Field2=mf.Field2 };
 return View("Index");
 }

模型

public class ModelF
 {
 public string Field { get; set; }
 [Range(typeof(DateTime), "1/1/2018", "1/1/2019")]
 public DateTime Field2 { get; set; }
	}

视图Index.cshtml

@using (Html.BeginForm("TestViewData", "Home"))
 {
 @Html.ValidationSummary()
 <input id="filed" name="Field2" type="text" placeholder="请输入" value="" />
 <input type="submit" value="提交" />
	}

测试,输入1/1/2020,执行结果为:

为了能显示字段的中文名称使用DisplayName

public class ModelF
 {
 public string Field { get; set; }
 [Range(typeof(DateTime), "1/1/2018", "1/1/2019")]
 [DisplayName("[这个字段]")]
 public DateTime Field2 { get; set; }
	}

执行结果为

2)设置标签特性值

由于class是C#保留关键字,因此设置class特性时要使用@

@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { @class="loginForm" }))
{ }

HTML辅助方法会将下划线渲染为连字符,因此要表达含有连字符的特性,那么使用下划线

Html.BeginForm("Login", "Account", FormMethod.Post, new { vla_input=true})

2.3加载分部视图

3 Url辅助方法

返回URI字符串

4 视图定位

· 视图放在Views文件夹下

· Views文件夹的子文件夹名称为控制器名称

· 视图名称可以是控制器操作方法名称也可以不是,若不是控制器操作方法名称,控制器返回视图时要指定视图名。

· Views文件夹下的Shared保存多个控制器共享的视图

视图定位规则是,先在Views文件夹中找对应控制器及控制器方法的视图,没有找到就到Shared文件夹下找。

5页面布局

· Views文件夹下_ViewStart.cshtml文件指定默认的模板,这个视图先于任何试图运行。

· 使用WebPageBase.Layout加载布局模板

· 使用@Html.Partial帮助方法加载部分视图

· 使用@section定义指定内容的节,然后使用WebPageBase.RenderSection加载指定的节,使用public HelperResult RenderSection(string name, bool required);required=true,那么节必须已经定义,否则抛异常。

· @Styles.Render和@Scripts.Render捆绑和压缩css、js

捆绑和压缩css与js

App_Start文件夹下BundleConfig类中

public static void RegisterBundles(BundleCollection bundles)
{
//多个文件用逗号分隔
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
 "~/Scripts/jquery.cookie.js",
 "~/Scripts/jquery.hoverDelay.js",
 "~/Scripts/jquery.pagination.js",
 "~/Scripts/jquery.form.js",
 "~/Scripts/json2.js",
 "~/Scripts/hydss.js",
	 "~/Scripts/hydss.utility.js"));
	
bundles.Add(new StyleBundle("~/Content/css/base").Include(
 "~/Content/css/common.css",
 "~/Content/css/dev.css"));
}

页面中使用已经捆绑并压缩的css和js,使用规则是:css文件置顶、js文件置地

@Styles.Render("~/Content/css/base")
@Scripts.Render("~/bundles/jqueryval")

覆盖默认布局模板

使用WebPageBase.Layout加载模板覆盖_ViewStart.cshtml文件指定默认的模板

例如:

_ViewStart.cshtml文件如下

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

Index.cshtml文件如下

@{
 ViewBag.Title="Home Page";
 Layout="~/Views/Shared/_LayoutOther.cshtml";
}
<div class="jumbotron">
	@*具体内容*@
</div>

如果没有Layout="~/Views/Shared/_LayoutOther.cshtml";这行代码,那么此视图文件将使用_ViewStart.cshtml中的_Layout.cshtml这个模板,但这里Index.cshtml文件使用的是另一个模板_LayoutOther.cshtml

使用实例

实际项目中可能会有不止一种布局,针对多种布局,既能满足这种需求要能尽可能地代码复用。

创建父模板_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
 <title>@ViewBag.Title</title>
 <link rel="icon" href="~/favicon.ico" />
 <link rel="shortcut Icon" href="~/favicon.ico" />
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <meta http-equiv="imagetoolbar" content="no" />
 <meta name="apple-mobile-web-app-capable" content="yes" />
 <meta name="keywords" content="词1 词2" />
 <meta name="description" content="网站的主题" />
 @Styles.Render("~/Content/css/base")
	@Scripts.Render("~/bundles/jquery")
	@*加载HeaderSection节*@
 @RenderSection("HeaderSection", false)
</head>
<body>
 @*加载主体*@
 @RenderBody()
 <img id="loading" style="display:none;position:fixed;top:50%;left:50%;" src="@Url.Content("~/Content/images/loading.gif")" title="加载中..." alt="加载中..." />
 <a href="javascript:void(0);" id="backToTop" title="回到顶部"></a>
 @*加载脚本*@
 @Scripts.Render("~/bundles/jqueryval")
 @*加载FooterSection节*@
	@RenderSection("FooterSection", false)
</body>
</html>

创建子模板_LayoutOther.cshtml

@{
 Layout="~/Views/Shared/_Layout.cshtml";
}
@*定义HeaderSection节*@
@section HeaderSection{
 @RenderSection("HeaderSection", false)
}
@*加载页头*@
@Html.Partial("_header")
@*加载主体*@
@RenderBody()
@*加载页脚*@
@Html.Partial("_footer")
@*定义FooterSection节*@
@section FooterSection{
 @RenderSection("FooterSection", false)
}

分析

_LayoutOther.cshtml视图使用了_Layout.cshtml视图文件,_Layout.cshtml中@RenderSection来加载FooterSection和HeaderSection节,而这个节定义在_LayoutOther.cshtml中,不过没有具体内容;_LayoutOther.cshtml中定义的FooterSection和HeaderSection又各自加载其他也面定义的FooterSection和HeaderSection节,所以可以在使用_LayoutOther.cshtml中灵活定义FooterSection和HeaderSection节,可以想象这样一个场景,每个页面都需要加载js文件,而他们既有共用的js文件,又有非共用的js文件,那么可以在使用_LayoutOther.cshtml的视图中定义section 节来加载只有此页面使用的js文件,而把公共的js文件放在_Layout.cshtml视图文件中

例如Index.cshtml定义@section FooterSection{

@Scripts.Render("~/bundles/index")

}

这个节加载只供Index.cshtml这个页面实用的js,这样其他不需要这个js的页面就不必加载这个js,从而达到减少页面加载文件的目的进而优化了页面。

6加载分部视图

1)控制器返回分部视图

配合@Html.Action方法使用控制器操作返回分部视图

视图中使用@Html.Action("TestPy"),控制器如下

public ActionResult TestFrom()
 {
 return PartialView("TestPy");
 }

或者在视图中使用@{Html.RenderAction("TestPy");},注意这种内联视图和Html.Action使用的区别。

2)使用html帮助方法

使用Html.Partial直接调用部分视图而不是通过控制器操作方法。

@Html.Partial("_header")

7视图向控制器传递数据

1)使用表单向控制器传递数据

视图代码

@using (Html.BeginForm("TestFrom", "Home"))
 {
 <input id="UserName" name="UserName" type="text" placeholder="请输入用户名" value="" />
 <input id="Password" name="Password" type="password" placeholder="请输入密码" value="" />
 <input type="submit" value="提交"/>
	}

控制器代码

public ActionResult TestFrom(FormCollection c)
 {
 var un=c["UserName"];
 var pw=c["Password"];
 ViewBag.Un=un;
 ViewBag.Pw=pw;
 return PartialView("TestPy");
 }

8自定义html辅助方法

返回值类型为IHtmlString,IHtmlString 是一个接口

 public static IHtmlString HYSubString(this HtmlHelper helper, string param)
 {
	//字符串
	String ret=......
 return helper.Raw(ret);
 }

参考:

1. Jess Chadwick/Todd Snyder/Hrusikesh Panda,徐雷/徐扬

译。ASP.NET MVC4 Web编程

2. Jon Galloway/Phil Haack/Brad Wilson/K. Scott Allen,孙远帅/邹权译 ASP.NET MVC4 高级编程(第四版)

3. ASP.NET MVC4开发指南,黄保翕

4. ASP.NET MVC4框架揭秘,蒋金楠

5. https://www.asp.net/mvc

置顶王》是图加软件提供的免费桌面工具!通过它可轻松将各种 Windows 窗体(包括无标题栏窗体)置顶或取消置顶。

使用方法

【一键置顶当前窗体】(视频教程)

在当前窗体的标题栏单击右键,在鼠标指针左边会出现一个“皇冠”图标;

将鼠标指针移入“皇冠”图标内,即可置顶当前窗体。

【一键取消置顶当前窗体】

在当前窗体的标题栏单击右键,在鼠标指针左边会出现一个“皇冠”图标;

将鼠标指针移入“皇冠”图标内,即可取消置顶当前窗体。

来源:http://www.3h3.com/soft/257678.html

天查Html手册时,又有了新的发现。也就这机会,好好总结下HTML中Meta的使用。

  HTML <meta> 标签,所有浏览器都支持 <meta> 标签。它提供关于HTML文档的元数据。元数据不会显示在页面上,但是对于机器是可读的。它可用于浏览器(如何显示内容或重新加载页面),对搜索引擎和更新频度的描述和关键词,或其他 web 服务。

  <meta> 标签位于文档的头部,不包含任何内容。<meta> 标签的属性定义了与文档相关联的名称/值对。

  在 HTML 中,<meta> 标签没有结束标签,在 XHTML 中,<meta> 标签必须被正确地关闭。

必要属性

属性值描述contentsome text定义与http-equiv或name属性相关的元信息

可选属性

属性值描述http-equivcontent-type / expire / refresh / set-cookie把content属性关联到HTTP头部。nameauthor / description / keywords / generator / revised / others把 content 属性关联到一个名称。contentsome text定义用于翻译 content 属性值的格式。

  • SEO优化

    关键词:类似这样的 meta 标签可能对于进入搜索引擎的索引有帮助.使用人们可能会搜索,并准确描述网页上所提供信息的描述性和代表性关键字及短语。标记内容太短,则搜索引擎可能不会认为这些内容相关,标记不应超过 874 个字符。

    <meta name="keywords" content="HTML,ASP,PHP,SQL">

    页面描述,每个网页都应有一个不超过 150 个字符且能准确反映网页内容的描述标签

  <meta name="description" content="your description">

    搜索引擎索引方式,robotterms是一组使用逗号(,)分割的值,通常有如下几种取值:none,noindex,nofollow,all,index和follow。确保正确使用nofollow和noindex属性值。

  

<meta name="robots" content="index,follow" />
<!--
all:文件将被检索,且页面上的链接可以被查询;
none:文件将不被检索,且页面上的链接不可以被查询;
index:文件将被检索;
follow:页面上的链接可以被查询;
noindex:文件将不被检索;
nofollow:页面上的链接不可以被查询。
-->

  页面重定向和刷新:content内的数字代表时间(秒),既多少时间后刷新。如果加url,则会重定向到指定网页(搜索引擎能够自动检测,也很容易被引擎视作误导而受到惩罚)。

  <meta http-equiv="Refresh" content="5;url=http://www.w3school.com.cn" /><!--5秒钟后跳转到http://www.w3school.com.cn-->

  <meta http-equiv="Refresh" content="5;" /><!--每5秒钟刷新一下页面-->

  • 移动设备

   viewport:能优化移动浏览器的显示。如果不是响应式网站,不要使用initial-scale或者禁用缩放。大部分4.7-5寸设备的viewport宽设为360px;5.5寸设备设为400px;iphone6设为375px;ipone6 plus设为414px。很多人使用initial-scale=1到非响应式网站上,这会让网站以100%宽度渲染,用户需要手动移动页面或者缩放。如果和initial-scale=1同时使用user-scalable=no或maximum-scale=1,则用户将不能放大/缩小网页来看到全部的内容。

      content 参数:

        width viewport 宽度(数值/device-width)
        height viewport 高度(数值/device-height)
        initial-scale 初始缩放比例
        maximum-scale 最大缩放比例
        minimum-scale 最小缩放比例
        user-scalable 是否允许用户缩放(yes/no)

  

<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=no"/>
<!-- `width=device-width` 会导致 iPhone 5 添加到主屏后以 WebApp 全屏模式打开页面时出现黑边 -->

  

  各浏览器平台


  Microsoft Internet Explorer

<!-- 优先使用最新的ie版本 -->
<meta http-equiv="x-ua-compatible" content="ie=edge">
<!-- 是否开启cleartype显示效果 -->
<meta http-equiv="cleartype" content="on">
<meta name="skype_toolbar" content="skype_toolbar_parser_compatible">

<!-- 关于X-UA-Compatible -->
<meta http-equiv="X-UA-Compatible" content="IE=6" ><!-- 使用IE6 -->
<meta http-equiv="X-UA-Compatible" content="IE=7" ><!-- 使用IE7 -->
<meta http-equiv="X-UA-Compatible" content="IE=8" ><!-- 使用IE8 -->

<!-- Pinned Site -->
<!-- IE 10 / Windows 8 -->
<meta name="msapplication-TileImage" content="pinned-tile-144.png">
<meta name="msapplication-TileColor" content="#009900">
<!-- IE 11 / Windows 9.1 -->
<meta name="msapplication-config" content="ieconfig.xml">  

  Google Chrome

<!-- 优先使用最新的chrome版本 -->
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<!-- 禁止自动翻译 -->
<meta name="google" value="notranslate"> 

  360浏览器

  <!-- 选择使用的浏览器解析内核 -->
<meta name="renderer" content="webkit|ie-comp|ie-stand">

  UC手机浏览器

  <!-- 将屏幕锁定在特定的方向 -->
<meta name="screen-orientation" content="landscape/portrait">
<!-- 全屏显示页面 -->
<meta name="full-screen" content="yes">
<!-- 强制图片显示,即使是"text mode" -->
<meta name="imagemode" content="force">
<!-- 应用模式,默认将全屏,禁止长按菜单,禁止手势,标准排版,强制图片显示。 -->
<meta name="browsermode" content="application">
<!-- 禁止夜间模式显示 -->
<meta name="nightmode" content="disable">
<!-- 使用适屏模式显示 -->
<meta name="layoutmode" content="fitscreen">
<!-- 当页面有太多文字时禁止缩放 -->
<meta name="wap-font-scale" content="no">

  QQ手机浏览器

<!-- 锁定屏幕在特定方向 -->
<meta name="x5-orientation" content="landscape/portrait">
<!-- 全屏显示 -->
<meta name="x5-fullscreen" content="true">
<!-- 页面将以应用模式显示 -->
<meta name="x5-page-mode" content="app">

  Apple iOS

<!-- Smart App Banner -->
<meta name="apple-itunes-app" content="app-id=APP_ID,affiliate-data=AFFILIATE_ID,app-argument=SOME_TEXT">
<!-- 禁止自动探测并格式化手机号码 -->
<meta name="format-detection" content="telephone=no">
<!-- Add to Home Screen添加到主屏 -->
<!-- 是否启用 WebApp 全屏模式 -->
<meta name="apple-mobile-web-app-capable" content="yes">
<!-- 设置状态栏的背景颜色,只有在 “apple-mobile-web-app-capable” content=”yes” 时生效 -->
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<!-- 添加到主屏后的标题 -->
<meta name="apple-mobile-web-app-title" content="App Title">

  Google Android

<meta name="theme-color" content="#E64545">
<!-- 添加到主屏 -->
<meta name="mobile-web-app-capable" content="yes">
<!-- More info: https://developer.chrome.com/multidevice/android/installtohomescreen -->
App Links

<!-- iOS -->
<meta property="al:ios:url" content="applinks://docs">
<meta property="al:ios:app_store_id" content="12345">
<meta property="al:ios:app_name" content="App Links">
<!-- Android -->
<meta property="al:android:url" content="applinks://docs">
<meta property="al:android:app_name" content="App Links">
<meta property="al:android:package" content="org.applinks">
<!-- Web Fallback -->
<meta property="al:web:url" content="http://applinks.org/documentation">
<!-- More info: http://applinks.org/documentation/ -->

  其它常用的meta

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="format-detection"content="telephone=no, email=no" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" /><!-- 删除苹果默认的工具栏和菜单栏 -->
<meta name="apple-mobile-web-app-status-bar-style" content="black" /><!-- 设置苹果工具栏颜色 -->
<meta name="format-detection" content="telphone=no, email=no" /><!-- 忽略页面中的数字识别为电话,忽略email识别 -->
<!-- 启用360浏览器的极速模式(webkit) -->
<meta name="renderer" content="webkit">
<!-- 避免IE使用兼容模式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 -->
<meta name="HandheldFriendly" content="true">
<!-- 微软的老式浏览器 -->
<meta name="MobileOptimized" content="320">
<!-- uc强制竖屏 -->
<meta name="screen-orientation" content="portrait">
<!-- QQ强制竖屏 -->
<meta name="x5-orientation" content="portrait">
<!-- UC强制全屏 -->
<meta name="full-screen" content="yes">
<!-- QQ强制全屏 -->
<meta name="x5-fullscreen" content="true">
<!-- UC应用模式 -->
<meta name="browsermode" content="application">
<!-- QQ应用模式 -->
<meta name="x5-page-mode" content="app">
<!-- windows phone 点击无高光 -->
<meta name="msapplication-tap-highlight" content="no">
<!-- 适应移动端end -->

  • 网页相关

  网页编码:以下两种charset定义方式均可


<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  禁止缓存:禁止浏览器从本地机的缓存中调阅页面内容,网页不保存在缓存中,每次访问都刷新页面。这样设定,访问者将无法脱机浏览

<meta http-equiv="Pragma" content="no-cache">

  网页过期:指定网页在缓存中的过期时间,一旦网页过期,必须到服务器上重新调阅。注意:必须使用GMT的时间格式,或直接设为0(数字表示多少时间后过期)

<Meta http-equiv="Expires" Content="Wed, 26 Feb 1997 08:21:57 GMT">

  Cookie设置:注意:必须使用GMT的时间格式

<Meta http-equiv="Set-Cookie" Content="cookievalue=xxx; expires=Wednesday,21-Oct-98 16:14:21 GMT; path=/">

  显示窗口的设定:强制页面在当前窗口以独立页面显示,这个属性是用来防止别人在框架里调用你的页面。Content选项:_blank、_top、_self、_parent.

<Meta http-equiv="Widow-target" Content="_top">

  进入与退出:这个是页面被载入和调出时的一些特效。这个有好多特效,可以查询Page-Exit去了解更多。

  <Meta http-equiv="Page-Exit" Content="blendTrans(Duration=0.5)">

  • 安全相关

  内容安全策略CSP(Content-Security-Policy),可以参考https://blog.csdn.net/u014465934/article/details/84199171

<meta http-equiv="Content-Security-Policy" content="script-src 'self'">



喜欢小编的可以点个赞关注小编哦,小编每天都会给大家分享文章。

我自己是一名从事了多年的前端老程序员,小编为大家准备了新出的前端编程学习资料,免费分享给大家!

如果你也想学习前端,可以观看【置顶】文章。也可以私信【1】拿