MVC

MCV设计模式概述:
作用:分离界面设计和业务逻辑。
MVC是Model-View-Controller的缩写,即“模型-视图-控制器”
MVC:模型(M)->视图(V)->控制器(C)

模型(Model)-->处理 模型代表了业务数据和业务逻辑
视图(View)-->输出 视图是用户看到并与之交互的界面
控制器(Controller)-->输入 逻辑处理、控制实体数据在视图上展示、调用模型处理业务请求

MCV的执行过程:
浏览器请求-->控制器(Controllers)【输入】-->模型(Models)【处理】-->视图(Views)【输出】-->显示结果

MVC与三层架构的区别:
MVC:是表现形式;三层架构:是架构模式。


MVC页面请求执行过程:
创建RouteTable-->URL路由-->执行MvcHandle-->执行Controller-->执行View()方法

创建MVC应用程序分为4个步骤:
1)创建模型类2)创建控制器类3)创建视图文件4)测试和运行


模型(M):应用对象。
模型是应用程序的主体部分。 模型代表了业务数据和业务逻辑; 当数据发生改变时,它要负责通知视图部分;
一个模型能为多个视图提供数据。由于同一个模型可以被多个视图重用,所以提高了应用的可重用性。

视图(V):数据的展现。
视图是用户看到并与之交互的界面。视图向用户 MVC设计模式中的三个模式结构显示相关的数据,并能接收用户
的输入数据,但是它并不进行任何实际的业务处理。视图可以向模型查询业务状态,但不能改变模型。视图还能
接受模型发出的数据更新事件,从而对用户界面进行同步更新。

控制器(C):
逻辑处理、控制实体数据在视图上展示、调用模型处理业务请求。

1、模型(Model) 模型是应用程序的主体部分。模型表示业务数据,或者业务逻辑.
2、视图(View) 视图是应用程序中用户界面相关的部分,是用户看到并与之交互的界面。
3、控制器(controller) 控制器工作就是根据用户的输入,控制用户界面数据显示和更新model对象状态。

Model:是一组类,用来描述被处理的数据,同时也定义这些数据如何被变更和操作的业务规则。与数据访问层
非常类似。

View:是一种动态生成HTML的模板,定义程序的用户界面如何显示。

Controller:是一组类,用来处理来自用户的信息,全部程序流和具体的程序逻辑,通常以Controller为后缀

控制器和视图的数据传递:
控制器-->视图(ViewDate,TempDate,Model)
视图-->控制器(Request.form,FormCollection,Model Binder)

扩展方法:
1)静态类2)静态方法3)this前缀关键字
如: public static class LabelExtensions
{
public static string Label(this HtmlHelper helper,string name,string text)
{
return String.Format("<h3><Label for='{0}'{1}</h3>",name,text);
}
}
BeginForm扩展方法主要实现表单定义的开始部分
如: <% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>

<fieldset>
<legend>商品信息</legend>

<%:Html.Hidden("id") %>

<%--<div class="editor-label">
<%: Html.LabelFor(model => model.Id) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Id) %>
<%: Html.ValidationMessageFor(model => model.Id) %>
</div>--%>

<div class="editor-label">
商品类型: <%-- <%: Html.LabelFor(model => model.CategoryId) %>--%>
</div>
<div class="editor-field">
<%:Html.DropDownList("CategoryId") %>


<%--<%: Html.TextBoxFor(model => model.CategoryId) %>
<%: Html.ValidationMessageFor(model => model.CategoryId) %>--%>
</div>

<div class="editor-label">
商品名称: <%-- <%: Html.LabelFor(model => model.Name) %>--%>
</div>
<div class="editor-field">

<%:Html.TextBox("Name") %>
<%-- <%: Html.TextBoxFor(model => model.Name) %>
<%: Html.ValidationMessageFor(model => model.Name) %>--%>
</div>

<div class="editor-label">
商品编号: <%-- <%: Html.LabelFor(model => model.SerialNumber) %>--%>
</div>
<div class="editor-field">
<%:Html.TextBox("SerialNumber")%> <%-- <%: Html.TextBoxFor(model =>
model.SerialNumber) %>
<%: Html.ValidationMessageFor(model => model.SerialNumber) %>--%>
</div>

<div class="editor-label">
成本价:<%-- <%: Html.LabelFor(model => model.CostPrice) %>--%>
</div>
<div class="editor-field">
<%:Html.TextBox("CostPrice")%> <%-- <%: Html.TextBoxFor(model =>
model.CostPrice) %>
<%: Html.ValidationMessageFor(model => model.CostPrice) %>--%>
</div>

<div class="editor-label">
销售价: <%-- <%: Html.LabelFor(model => model.SalePrice) %>--%>
</div>
<div class="editor-field">
<%: Html.TextBox("SalePrice") %> <%--<%: Html.TextBoxFor(model =>
model.SalePrice) %>
<%: Html.ValidationMessageFor(model => model.SalePrice) %>--%>
</div>

<div class="editor-label">
单位: <%--<%: Html.LabelFor(model => model.Unit) %>--%>
</div>
<div class="editor-field">
<%:Html.TextBox("Unit") %> <%-- <%: Html.TextBoxFor(model => model.Unit) %>
<%: Html.ValidationMessageFor(model => model.Unit) %>--%>
</div>

<p>
<input type="submit" value="添加保存" />
</p>
</fieldset>

<% } %>

<div>
<%: Html.ActionLink("返回", "Index") %>
</div>

绑定上传文件:Upload.aspx
<div>
<h2>文件上传</h2>
<% using(Html.BeginForm("Upload", "Home", FormMethod.Post, new
{ enctype="multipart/form-data"}))
{ %>
<input name="upload" type="file" /> <input type="submit" value="上传文件
" />
<%} %>
</div>

[HttpPost]
public ActionResult Upload(HttpPostedFileBase upload)
{
--string fileName = Path.GetFileName(upload.FileName);
upload.SaveAs(Server.MapPath("~/Content/" + upload.FileName));
return Content("上传成功");
}


mvc3.0:新特性s:
Razor 视图引擎,支持多视图引擎,Controller 改进
Unobtrusive JavaScript、jQuery Validation和JSON绑定带来更加丰富的JavaScript支持;Model 验证的改
进,依赖注入 Dependency Injection 的改进,
新的依赖注入(Dependency Injection)和Global Action Filters带来更强大的hooks;
完善后的模型验证(Model Validation)提供了更流畅的验证。

MVC4.0:新特性:
Web API。
增强的项目模版。
移动项目模板使用jQuery Mobile。
显示模式(Display Modes)
异步控制器。
Bundling and minificat

mvc3:Insert Update Delete Insert
http://developer.51cto.com/art/201203/323237.htm

原文地址:https://www.cnblogs.com/shanzzs/p/MVC.html