MVC:  Model,View,Control

 

设置View中的数据

1. 返回model,View中强类型化

Control:

public ActionResult Browse(string Genre)
       {
           var Album = db.Genres.Include("Albums").Single(c => c.Name == Genre);
           ViewData["Genre"] = Genre;
           return View(Album);
       }

 

View: 

  @model Mvc_MusicShop_diy.Models.Genre   (强类型化,一个View只能强类型化一个)

类似的 当 control 返回  list<xx>集合,view 应当如下强类型化

@model List<Mvc_MusicShop_diy.Models.Genre> 

or 

@model  IEnumerable<Mvc_MusicShop_diy.Models.Genre>

使用:

<ul>
@foreach (var Genre in Model)
{
<li>
@Html.ActionLink(@Genre.Name, "Browse", new { Genre=@Genre.Name})
</li>

}
</ul>

 

 

2.  ViewData

Control 设置:ViewData["Provinces"] = db.Provinces.ToList();

View使用:      @using Mvc_MusicShop_diy.Models

                               ViewData["Provinces"]   as  List<Province>

 

3.  ViewBag

ViewBag.title=”购物车页面”   //设置View 页面的html元素的值

 

 

设置模版View

1._ViewStart.cshtml 代码制定了页面的默认模版的路径

@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
 
查看模版页面代码 
 
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
</head>
<body>
    <div class="page">
        <div id="header">
            <div id="title">
                <h1>欢迎光临南京网上商城</h1>
            </div>
            <div id="logindisplay">
                @Html.Partial("_LogOnPartial")
            </div>
            <div id="menucontainer">
                <ul id="menu">
                    <li>@Html.ActionLink("商城主页", "Index", "Home")</li>
                    <li>@Html.ActionLink("关于商城", "About", "Home")</li>
                    <li>@{Html.RenderAction("CartSummary", "ShoppingCart", "");}</li>
 
                </ul>
            </div>
        </div>
       
        @*@{Html.RenderAction("Category", "Home");}*@
       
        <div id="main">
            @RenderBody()
        </div>
        <div id="footer">
        关于我们|联系我们|人才招聘|商家入驻|广告服务|手机京东|友情链接|销售联盟|商城社区|南京商城公益
        </div>
    </div>
</body>
</html>
 
 
注意: 所有view默认模版代码里,有完整的 html 标记 ,包含title,body
         @RenderBody() 占位符是view页面的代码所在的位置
 
 
 
2.  通用模版 

[ChildActionOnly]    //表明了,通过url :  /control/CartSummary 访问是不存在的;只作为其他View的一部分

       public ActionResult CartSummary()

       {

           var cart = ShoppingCart.GetCart(this.HttpContext);

                       ViewData["CartCount"] = cart.GetItemsCount();

                       return PartialView();  //或者 PartialView("CartSummary");

       }

 

在 _ViewStart.cshtml 中使用

<li>   @{Html.RenderAction("CartSummary", "ShoppingCart", "");}  </li>

注意:@{     }

 

 

MVC3自带的客户端验证和服务端验证

原理:model与view 模型 绑定

必须引用:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>

<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

 

 

@Html.ValidationSummary(true)  在指定地方将错误集中展示  ,生成的html代码为

  <ul><li>错误1</li><li>错误2</li></ul>

 

@Html.ValidationMessageFor(model => model.Title)     展示title出错的信息(根据model中类型与属性自动 js 判断和服务端判断)

 

服务端根据 ModelState.IsValid   Bool 的值 (根据model相关约束验证后是否有错误)

   [HttpPost]
        public ActionResult Create(Album album)
        {
            
            if (ModelState.IsValid)
            {
                db.Albums.Add(album);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }
 
            return View(album);
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

 

Model 中添加自定义约束

比如: 某不能为空,若为空提示自定义的错误消息;view中显示的元素名称为XX?email,phone 正则验证;

首先得引用命名空间

using System.ComponentModel;

using System.ComponentModel.DataAnnotations;

 

[DisplayName("手机号")]

[Required(ErrorMessage = "手机号不能为空")]

[RegularExpression(@"^1[3|4|5|8]\d{9}$", ErrorMessage = "手机号格式错误")]

public  string phone{get;set;} 

[ScaffoldColumn(false)]   //主键;当与view模型绑定生成自动生成html元素的时候,是否隐藏该列

  public int AddressId { get; set; }

 

 

 

最新文章

  1. PHP 数组浅析
  2. 浅谈我对C#中抽象类与接口的理解
  3. Make Helix Curve in OpenCASCADE
  4. fatal error: &#39;XCTest/XCTest.h&#39; file not found
  5. 使用UltraEdit实现从DOS文件到UNIX文件的批量转换
  6. 从两个平方算法到分治算法-java
  7. 解决Firefox浏览器每次打开都弹出导入向导的问题
  8. 关于调整浏览器窗口JS
  9. UVALive 5075 Intersection of Two Prisms(柱体体积交)
  10. ODBC错误处理
  11. Tomcat7中配置Oracle 11g数据库DBCP连接池
  12. ububtu 彻底卸载程序的几种方法
  13. 独立搭建zookeeper
  14. Ubuntu文件的复制、移动和删除命令
  15. Java调用C# DLL
  16. Linux网络编程学习(七) ----- 有名管道(第四章)
  17. 通过HTTP服务访问FTP服务器文件(配置nginx+ftp服务器)
  18. 前后分离模型之封装 Api 调用
  19. The Microservices Workflow Automation Cheat Sheet
  20. (转)深入sql server中的事务

热门文章

  1. Java源码之String
  2. Qt 如何获取一个文件的 Icon 图标?
  3. AFNetworking 和 ASIHTTPRequest
  4. MySQL数据库(3)_MySQL数据库表记录操作语句
  5. Python基础(19)_异常处理
  6. Python基础(8)_迭代器、生成器、列表解析
  7. SQL SREVER, ORACLE数据库连接字符串
  8. 前端之JQuery [续]
  9. Object.defineProperty小解
  10. codeforces上某题