.Net MVC framework basics

First, what is MVC?

Create a Web application design patterns MVC is a (- - Model View Controller Model View Controller) using MVC.

MVC full name Model View Controller, the model (model) - view (view) - Abbreviation controller (Controller), and a software design model, with a service logic, data, a method of separating tissue interface display code, business logic to gather a member which, while improving and customization interfaces and user interaction does not need to rewrite the business logic. MVC was developed a unique structure for a conventional mapping input, processing and output of the logic functions in a graphical user interface.

Model (model) is a portion for processing the application logic of the application data. Model objects are usually responsible for accessing data in the database. View (View) is a partial processing data applications. Usually view is created based on the model data. Controller (Controller) is a part of the application processing user interaction. Typically the controller is responsible for reading data from the view, a user input control, and the transmission data model.

A -1, understanding the structure of the frame MVC

Model (model) represents the core of the application (such as a database record list). View (View) display data (database record). Controller (controller) for input (write a database record).

MVC model while providing full control over HTML, CSS and JavaScript.

Second, what is the Model? (Model in MVC role)

Model (model) is a portion for processing the application logic of the application data. Model objects are usually responsible for accessing data in the database. ? All of the models should be placed under the Models directory 

Third, what is the view? And related knowledge

View(视图)是应用程序中处理数据显示的部分。通常视图是依据模型数据创建的。 所有的视图文件都应该放到Views目录下. 如果在相应的视图目录中没有找到视图文件,那么会寻找Views\Shared目录下名称相同的视图文件 在ASPX视图引擎中,可以使用asp服务器控件,但它的作用仅仅局限与生成html代码,而不推荐开发人员使用asp控件的事件.因此建议开发人员手工编写Html标记。 但是手写Html标记比较耗费时间,有没有更好的解决方案?答案就是使用Html辅助方法。 Html辅助方法的作用就是通过调用C#方法的方式,快速的生成相应的html标记. Html辅助方法分为2大类:弱类型html辅助方法和强类型html辅助方法. 其中以For关键字结尾的都是强类型的辅助方法.下面的示例代码演示了二者的调用方式,以及生成的html源码。          

 

附:常用Html辅助方法汇总

 

四、什么是Controller?(Controller在mvc中所起的作用)

Controller(控制器)是应用程序中处理用户交互的部分。通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。 所有的控制器必须放到Controllers文件夹下; Controller必须为公开类; 所有的控制器类名必须以Controller结尾; 所有的控制器类都应该继承Controller基类或实现IController接口的类; 在Action中return view(),默认会返回和Action名称一样的视图文件. 在控制器类中的返回值为ActionResult的公开方法,这些方法在MVC中称为Action。

四-(1)、Controller中的Action

Action用于响应用户的各种请求,比如向客户端返回html文档、html片段、json数据、纯文本、文件等结果。在ASP.NET MVC中通过在Action(行为或操作)方法中返回ActionResult类型的对象来实现向客户端响应上面的各种结果。

ActionResult类是个抽象类(抽象类无法直接被实例化). ActionResult有很多实现类,不同的实现类用于响应客户端不同的结果。并且在Controller类中提供了大量的辅助方法,这些辅助方法可以快速的创建各种ActionResult. 下面是详细介绍( ActionResult 的实现类)。

*ViewResult类 ViewResult类的作用是向客户端响应View目录中一个视图文件. Controller类中提供了View()方法,来快速的返回一个ViewResult类对象. 如:Return view (“index”); *ContentResult类 该类用于向客户端返回一段文本内容(纯文本\HTML...). 在Controller类中的辅助方法为:Controller.Content(string  content);

*RedirectToRouteResult类 该类的作用是从当前action跳转至其他action. 在Controller类中的辅助方法为:RedirectToAction(“ActionName”, “ControllerName”) *RedirectResult类 在服务器端从当前Action跳转到其他URL地址. 在Controller类中的辅助方法为:Controller.Redirect(string  url)

JsonResult类 该类用于向客户端返回一段Json格式的字符串. 在Controller类中的辅助方法为:Controller.Json(object  data); 注意:默认情况下mvc拒绝响应以get方式发送的ajax请求,需要使用JsonRequestBehavior.AllowGet。

*PartialViewResult类 该类的作用是向客户端响应Views目录的一个分部视图文件。分部视图就是只包含html片段的视图文件. 在Controller类中的辅助方法为:Controller.PartialView();

*FilePathResult类 该类用于实现文件下载. 在Controller类中的辅助方法为:Controller.File(string  filePath,  string  contentType, string  fileName);

 

四—(2)、如何在Action中获取表单提交的数据?

诸如新增\修改等功能模块,我们往往会创建2个名称相同的Action:一个action用于加载新增\修改页面;另一个action用于处理新增\修改页面提交的表单数据.那么如何区分在何时调用哪个action呢? 每种操作都对应两个方法重载,其中前面第一个没有特性前缀是HTTP GET模式访问服务器的,而第二个带有[HttpPost] attribute的方法是使用HTTP POST方式向服务器提交数据的。   第一个GET方法用来显示初始化的界面,并处理客户端验证;这时如果用户数据有误,就根本不会进入第二个POST方法。   如果JS被浏览器禁用,第一个GET方法只用来显示初始化界面,客户端验证失效,才会进入第二个Post方法

表单中提交的数据,可以通过Action的参数进行映射. 映射的前提条件:Html标记的name属性值和action中参数的名称要一致. 下面的示例代码演示了映射的过程(注意粉色框标记的部分):

我们还可以有另外一种等价的写法,就是将映射的属性定义到一个实体类中,这样我们就可以在action中添加该实体类型的参数,参考代码如下所示:

小结:MVC三大组件的相互关系

 

在控制器中可以直接调用视图和模型

在视图中可以调用模型。

模型不能调用视图。

模型能够限定视图中使用的数据,但视图中使用的模型应由控制器提供。

在视图中可以调用控制器(通过视图中表单的提交和点击超链接的方式调用)。

 

Guess you like

Origin www.cnblogs.com/wupeng6/p/11368345.html