学习第一天@MVC笔记

今天搞明白了五个问题:

列表:

1. 为什么要使用MVC

2.怎么在浏览器中调用控制器的方法

3. MVC3add controller时没有scaffolding options

4提供程序未返回ProviderManifestToken字符串

5.如何使用Entity Framework 的Code First

 

问题:

为什么要使用MVC

使用MVC的好处:

首先,多个视图能共享一个模型。

其次,控制器是自包含(self-contained)指高独立内聚的对象,与模型和视图保持相对独立,所以可以方便的改变应用程序的数据层和业务规则。

此外,控制器提高了应用程序的灵活性和可配置性。

 

问题:

MVC3中,运行之后会变成:找不到文件

即“/”应用程序中的服务器错误。

无法找到资源。HTTP404

输入控制器类名(注意:不允许重载,若要重载,则需要分别使用不同的特性:

如:

即【HttpPost】

public string welcome(string name,int a)

{

  return “hello everyone”;

}

[HttpGet]

public string welcome(string name)

{

  return “hello “ + name;

}

此时是由于没有改写的Index()方法
     

 

出现错误:

“/“应用程序中的服务器错误

The view “Index” or its master was not found or no view engine supports the searched locations.The following locations were searched:

~/View/hello/Index.aspx

~/View/hello/Index.ascx

……

使用方法:

        public string Index()

        {

            return "hello world!";

        }

替换原来同名方法:

public ActionResult Index()

        {

            return View();

        }

,直接输入控制器名就可以有默认的页面。

 

 

 

在目录下面再增加方法名:如welcome

即http://localhost:1747/hello/welcome

方法可以带参数:

即:http://localhost:1747/hello/welcome?name=xiaoming

 

问题:

MVC3add controller时没有scaffolding options,只有简单的一个选项,跟MVC2类似

 

 

需要安装 ASP.NET-MVC-3-ToolsUpdate,地址:http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=82CBD599-D29A-43E3-B78B-0F863D22811A

安装完毕.

关闭VS,重启,打开,在Controller中添加新Controller:

OK,搞定!

问题:

即Unable to retrieve metadata for ‘MvcHello.Models.Movie’.提供程序未返回ProviderManifestToken字符串。

原因已经查明,实在是很无语:

找到web.config的连接字符串的节点connectionStrings,修改Integrated Security属性:

Integrated Security=false;

是错误的。当值为false是则为SQL用户验证,否则是Windows登陆。

问题:

只有控制器类,而还没有数据库及数据:

现在还没有数据库,不过没关系,运行一下就会搞定!

运行,输入:

即:http://localhost:1747/movie

运行错误!

“/“应用程序中的服务器错误。

无法找到资源。HTTP 4040.

原因是:没有搞对控制器类名

输入之后,运行成功,有Index及数据的列表,修改,即已经实现了增删查改。

点击“创建”之后:

即可创建数据。

填入数据:

实在是太棒了!这个Entity Framework的Code First!!!!

原文地址:https://www.cnblogs.com/xiaxiazl/p/2339516.html