1.js中获得根目录
var AP_ROOT = '@Url.Content("~/")';
2.$.each([0,1,2],function(i,n){alert("item"+i+":"+n)});
3.App_Start文件夹中含有5个配置项
1)AuthConfig.cs 安全
2) BundleConfig.cs 捆绑和微小框架
cshtml中调用@Script.Render("~/bundles/jquery");参考BundleConfig
3)FilterConfig.cs
4)RouteConfig.cs
5)WebApiConfig.cs
4.继承Controller的都是控制器,可以再文件任意位置
5.Return(“Controller中方法”) 跳转页面
6.dynamic用法:
<ul>
@foreach(dynamic p in ViewBag.Albums){
<li>@p.Title</li>
}
</ul>
7.Controller返回List类型
var albums=new List<Album>();
....
return View(albums);
前台调用@model IEnumerable<...Models.Album>
@foreach(Album p in Model)
8.如果视图中经常使用namespace
则在web.config中进行声明
<namespaces>
...
<add namespace="MvcApplication1.Models" />
</namespaces>
9.@@转义字符@
10.模板页_Layout.cshtml <div>@RenderBody()</div>
子页调用@{
_Layout="~/views/shared/_Layout.cshtml";
}
为了消除这种冗余,MVC调用了_ViewStart.cshtml来进行统一的调用
11.特性的模板
模板页: @RenderSection("Footer",false);
子页:@Section Footer{
<section>............</section>
}
12.模板页中
@if(IsSectionDefined("Footer")){
RenderSection("Footer");
}
else{
<span>..............</span>
}
13.通常,在web应用程序中,GET请求用于读操作,POST请求用于写操作。
14.{Html.RenderPartical("ControllerMethod")}
Html.Partical("ControllerMethod")
15.在model中验证注解
命名空间:System.ComponentModel.DataAnnotations
1)[Required]
Public string FirstName{get;set;}
2)[StringLenth(160,MinimumLength=3)]
3)[RegularExpression(@"[a-z]...")]
4)[Range(35,48)]
自定义错误提示:
[Range(35,48),ErrorMessage="Your {0} should be >35 <48"]
16.为了国际化:
假设存在ErrorMessage.resx
条目1.LastNameRequired
条目2.LaseNameTooLong
[Required(ErrorMessageResourceType=typeof(ErrorMessage),ErrorMessageResourceName="LastNameRequired")]
17.假设用户在没有填写lastname的情况下提交了表单
ModelState.IsValid==false
ModelState.IsValidField("LastName")==false
ModelState["LastName"].Errors.Count>0
查看错误的提示信息
var ErrorMessage=ModelState["LastName"].Errors[0].ErrorMessage;
前台:@Html.ValidateMessageFor(m=>m.LastName)