ASP.NET + MVC5 入门完整教程三 (上) ---第一个MVC项目

article/2025/10/19 13:34:07

                                第一个MVC应用程序

1创建MVC项目

打开VS ,File--新建--项目,选择ASP Web项目,命名后确认。选择(Empty)空模板,

项目创建完成,会看到 解决方案管理器 窗口显示一些文件夹,如图,这是一个MVC的默认结构

2  添加第一个控制器


右键 解决方案中的“Controllers”文件夹,从弹出菜单选择 “添加”->“控制器”如上图所示;

添加后出现下图,单击“Add(添加)”按钮

这是打开 控制器对话框,命名为“Home Controller”,点击添加。

VS会在Controllers文件夹下创建一个新的C#文件,名称为"Home Controller.cs",这个类如下图所示;

3 渲染Web界面

创建web界面,在Index界面任意地方右键,从弹出菜单选择“Add View(添加视图)”,如下图:

Index.cshtml 基本内容如下所示:


我们看到:

@{Layout = null;
}

这是一个将由Razor视图引擎进行解释的表达式,Razor引擎处理视图内容并生成发送给浏览器的HTML。这是一个简单的Razor表达式,他告诉Razor未选用布局,现在我们暂时忽略,以后在详细介绍。对该页面添加内容。


调试后出现界面如下

4 添加动态输出

Web应用程序平台的关键时构造并显示动态输出。在MVC中。控制器的工作时构造一些数据并传递给视图,而视图则负责把他渲染成HTML。


将数据从控制器传递给视图的一种方式是使用 ViewBag (视图包)对象,他是Controller基类的一个成员。ViewBag 是一种动态对象,看可以给他赋任意属性,这些属性在随后渲染的视图中可用。下面演示了在HomeController.cs 文件中传递一些简单的动态数据。

public ViewResult Index(){int Hour = DateTime.Now.Hour;ViewBag.Greeting = Hour < 12 ? "Good Morning" : "Good afternoon";return View();}

当ViewBag.Greeting 属性进行赋值时,便是为视图提供数据,Greeting属性直到对其赋值的那一刻才会形成。为了获取到传递的数值,在Index.cshtml 文件做相应修改。

@{Layout = null;
}<!DOCTYPE html><html>
<head><meta name="viewport" content="width=device-width" /><title>Index</title>
</head>
<body><div> @ViewBag.Greeting World(From the view)</div>
</body>
</html>

注意:Greeting 可以是任意名称,你也可以写成 @ViewBag.name 只要和Index界面对应就可以实现值传递。

效果如下:

5 创建一个简单的数据录入程序

场景设置:假设朋友准备举办一场聚会,设计一个Web应用程序,对受邀人进行电子回复(RSVP);

  • 一个显示晚会信息首页
  • 一个用来回复的(PVSP)的表单
  • 对RVSP表单验证,显示一个感谢画面

界面 Views/Home/Index.cshtml 文件添加内容:

@{Layout = null;
}<!DOCTYPE html><html>
<head><meta name="viewport" content="width=device-width" /><title>Index</title><link href="~/Content/Style.css" rel="stylesheet" /><link href="~/Content/bootstrap.css" rel="stylesheet" /><link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body><div class="text-center">@ViewBag.Greeting World(From the view)<h2>我们将会有一个愉悦的party</h2><h3>您是否参加?</h3><div class="btn btn-success">@Html.ActionLink("PVSP Now", "RvspForm")</div></div>
</body>
</html>

设计一个数据模型:


添加模型类:

MVC 约定是将建立模型的类放在“Models”文件夹下,该文件夹是建立项目是自动创建的,右键“解决方案”窗口文件夹“Models”,从弹出窗选择“添加”--“类”,文件名设置为“GuestResponse.cs”,单击添加按钮,创建这个类编辑如下:


链接动作方法

在Index.cshtml 添加一个指向RSVP表单的链接;

@{Layout = null;
}<!DOCTYPE html><html>
<head><meta name="viewport" content="width=device-width" /><title>Index</title><link href="~/Content/Style.css" rel="stylesheet" /><link href="~/Content/bootstrap.css" rel="stylesheet" /><link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body><div class="text-center">@ViewBag.Greeting World(From the view)<h2>我们将会有一个愉悦的party</h2><h3>您是否参加?</h3><div class="btn btn-success">@Html.ActionLink("PVSP Now", "RvspForm")</div></div>
</body>
</html>

Html.ActionLink 是HTMLde 辅助器方法 MVC框架附带一组内置的辅助器方法。可以方便地用来渲染HTML的链接,文本输入,复选框以及其他内容。ActionLink一共两个参数,一个是显示文本,第二个是用户单击该连接产生的动作。此时单击该链接会报404错误,因为还没有 /Home/RsvpForm 该界面。此时,在HomeController类中添加一个“RsvpForm”的方法完成。


添加强类型视图






建立表单

编辑这个RvspForm.cshtml 。

@model MVCStudy.Models.GuestResponse@{Layout = null;
}<!DOCTYPE html><html>
<head><meta name="viewport" content="width=device-width" /><title>RvspForm</title><link href="~/Content/bootstrap.css" rel="stylesheet" /><link href="~/Content/bootstrap.css" rel="stylesheet" /><link href="~/Content/bootstrap-reboot.css" rel="stylesheet" /><link href="~/Content/bootstrap-reboot.min.css" rel="stylesheet" /><style>.btn a {color:white;text-decoration:none}body{background-color:#F1F1F1;}</style>
</head>
<body><div class="p"></div>@using (Html.BeginForm()){@Html.ValidationSummary()<div class="form-group"><label>Your Name :</label>@Html.TextBoxFor(x => x.Name, new { @class = "formcontrol" })</div><div class="form-group"><label>Your Email :</label>@Html.TextBoxFor(x => x.Email, new { @class = "formcontrol" })</div><div class="form-group"><label>Your phone : </label>@Html.TextBoxFor(x => x.Phone, new { @class = "formcontrol" })</div>      <div class="form-group"><label>是否接受邀请?</label> @Html.DropDownListFor(x => x.WillAttend, new[]{ new SelectListItem(){ Text="是,接受邀请",Value=bool.FalseString},new SelectListItem(){ Text = "否,不接受邀请", Value = bool.FalseString } },"请选择",new {@class="formcontrol"})</div><div class="text-center"><input class="btn btn-success" type="submit" value="提交"></div>}    
</body>
</html>


设置启动URL


注意:保持特定页空白


处理表单



请求,来调用合适的方法。对HomeController类做修改。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCStudy.Models;namespace MVCStudy.Controllers
{public class HomeController : Controller{// GET: Home     public ViewResult Index(){int Hour = DateTime.Now.Hour;ViewBag.Greeting = Hour < 12 ? "Good Morning" : "Good afternoon";return View();}[HttpGet]public ViewResult RvspForm(){return View();}[HttpPost]public ViewResult RvspForm(GuestResponse guestResponse){return View("Thanks",guestResponse);}}
}


using MVCStudy.Models 命名空间,这样可以直接使用GuestResponse模型类型,而不需要使用这个类的限定名。

使用模型绑定



渲染其他视图


View\Home\Thanks.cshtml。编辑此视图。

@model MVCStudy.Models.GuestResponse@{Layout = null;
}<!DOCTYPE html><html>
<head><meta name="viewport" content="width=device-width" /><link href="~/Content/bootstrap.css" rel="stylesheet" /><title>Thanks</title><style>body {background-color: #F1F1F1;}</style>
</head>
<body>@try{WebMail.SmtpServer = "smtp.example.com";WebMail.SmtpPort = 587;WebMail.EnableSsl = true;WebMail.UserName = "mySmtpUsername";WebMail.Password = "mySmtpPassword";WebMail.From = "rsvps@example.com";WebMail.Send("party-host@example.com", "RSVP Notifiaction", Model.Name + "is" + ((Model.WillAttend ?? false) ? "" : "not") + "attending");}catch(Exception){@:<b>对不起,未能给您发送回复邮件</b>}<div class="text-center"><h1>Thank you,@Model.Name</h1><div class="lead">@if (Model.WillAttend == true){@:感谢您的到来}else{@:您未能参加我们的Party,我们深感遗憾,感谢您的回复。}</div></div>
</body>
</html>

添加验证


注释属性进行定义。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace MVCStudy.Models
{public class GuestResponse{[Required(ErrorMessage ="请确认您的姓名")]public string Name { get; set; }[Required(ErrorMessage = "请确认您的邮箱")][RegularExpression(".+\\@.+\\..+",ErrorMessage ="请输入有效邮箱")]public string Email { get; set; }[Required(ErrorMessage = "请确认您的电话")]public string Phone { get; set; }[Required(ErrorMessage = "请确认您是否接受邀请")]public bool? WillAttend { get; set; }}
}




ValidationSummary()(验证摘要)辅助器方法。

@model MVCStudy.Models.GuestResponse@{Layout = null;
}<!DOCTYPE html><html>
<head><meta name="viewport" content="width=device-width" /><title>RvspForm</title><link href="~/Content/bootstrap.css" rel="stylesheet" /><link href="~/Content/bootstrap.css" rel="stylesheet" /><link href="~/Content/bootstrap-reboot.css" rel="stylesheet" /><link href="~/Content/bootstrap-reboot.min.css" rel="stylesheet" /><style>.btn a {color:white;text-decoration:none}body{background-color:#F1F1F1;}</style>
</head>
<body><div class="p"></div>@using (Html.BeginForm()){@Html.ValidationSummary()<div class="form-group"><label>Your Name :</label>@Html.TextBoxFor(x => x.Name, new { @class = "formcontrol" })</div><div class="form-group"><label>Your Email :</label>@Html.TextBoxFor(x => x.Email, new { @class = "formcontrol" })</div><div class="form-group"><label>Your phone : </label>@Html.TextBoxFor(x => x.Phone, new { @class = "formcontrol" })</div>      <div class="form-group"><label>是否接受邀请?</label> @Html.DropDownListFor(x => x.WillAttend, new[]{ new SelectListItem(){ Text="是,接受邀请",Value=bool.FalseString},new SelectListItem(){ Text = "否,不接受邀请", Value = bool.FalseString } },"请选择",new {@class="formcontrol"})</div><div class="text-center"><input class="btn btn-success" type="submit" value="提交"></div>}    
</body>
</html>

高亮显示无效字段

的内容:

.field-validation-error {color: #f00;
}
.field-validation-valid{display:none;}
.input-validation-error{border:1px solid #f00;background-color:#fee;}
.validation-summary-errors{font-weight:bold;color:#f00}
.validation-summary-valid{display:none}

在 RvsvpForm.cshtml中添加Link元素。

直接从解决方案中用鼠标拖拽文件到相应位置就能自动写Link.

设置内容样式

使用NuGet安装Bootstrap;

找到Bootstrap安装即可。

设置Index视图

@{Layout = null;
}<!DOCTYPE html><html>
<head><meta name="viewport" content="width=device-width" /><title>Index</title><link href="~/Content/Style.css" rel="stylesheet" /><link href="~/Content/bootstrap.css" rel="stylesheet" /><link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body><div class="text-center">@ViewBag.Greeting World(From the view)<h2>我们将会有一个愉悦的party</h2><h3>您是否参加?</h3><div class="btn btn-success">@Html.ActionLink("PVSP Now", "RvspForm")</div></div>
</body>
</html>

设置RsvpForm视图

@model MVCStudy.Models.GuestResponse@{Layout = null;
}<!DOCTYPE html><html>
<head><meta name="viewport" content="width=device-width" /><title>RvspForm</title><link href="~/Content/Style.css" rel="stylesheet" /><link href="~/Content/bootstrap.css" rel="stylesheet" /><style>.btn a {color:white;text-decoration:none}body{background-color:#F1F1F1;}</style>
</head>
<body><div class="text-center">@using (Html.BeginForm()){@Html.ValidationSummary()<div class="form-group"><label>Your Name :</label>@Html.TextBoxFor(x => x.Name, new { @class = "formcontrol" })</div><div class="form-group"><label>Your Email :</label>@Html.TextBoxFor(x => x.Email, new { @class = "formcontrol" })</div><div class="form-group"><label>Your phone : </label>@Html.TextBoxFor(x => x.Phone, new { @class = "formcontrol" })</div><div class="form-group"><label>是否接受邀请?</label>@Html.DropDownListFor(x => x.WillAttend, new[] { new SelectListItem() { Text = "是,接受邀请", Value = bool.FalseString }, new SelectListItem() { Text = "否,不接受邀请", Value = bool.FalseString } }, "请选择", new { @class = "formcontrol" })</div><div class="text-center"><input class="btn btn-success" type="submit" value="提交"></div>}</div> 
</body>
</html>



设置Thanks视图样式

@model MVCStudy.Models.GuestResponse@{Layout = null;
}<!DOCTYPE html><html>
<head><meta name="viewport" content="width=device-width" /><link href="~/Content/bootstrap.css" rel="stylesheet" /><title>Thanks</title><style>body {background-color: #F1F1F1;}</style>
</head>
<body>@try{WebMail.SmtpServer = "smtp.example.com";WebMail.SmtpPort = 587;WebMail.EnableSsl = true;WebMail.UserName = "mySmtpUsername";WebMail.Password = "mySmtpPassword";WebMail.From = "rsvps@example.com";WebMail.Send("party-host@example.com", "RSVP Notifiaction", Model.Name + "is" + ((Model.WillAttend ?? false) ? "" : "not") + "attending");}catch(Exception){@:<b>对不起,未能给您发送回复邮件</b>}<div class="text-center"><h1>Thank you,@Model.Name</h1><div class="lead">@if (Model.WillAttend == true){@:感谢您的到来}else{@:您未能参加我们的Party,我们深感遗憾,感谢您的回复。}</div></div>
</body>
</html>

源码下载:https://download.csdn.net/download/qq_21419015/10433092


http://chatgpt.dhexx.cn/article/ocYjqpLR.shtml

相关文章

ASP.net MVC5 简介

ASP.net MVC5 简介 MVC是什么 MVC是一种设计模式。 也就是Model-View-Controller 模型-视图-控制器。 Model的功能 Model试数据模型&#xff0c;用来封装与程序相关的数据&#xff0c;以及对数据操作的处理方法。 有关“数据处理”的范围都可以属于Model&#xff0c;比如…

ASP.NET MVC5 网站开发实践 - 概述

前段时间一直在用MVC4写个网站开发的demo&#xff0c;由于刚开始学所有的代码都写在一个项目中&#xff0c;越写越混乱&#xff0c;到后来有些代码自己都理不清了。1月26日晚上在群里跟怒放 他们讨论这个问题&#xff0c;结论是即使只是一个小demo也应该分层来写。正好看到别人…

MVC5路由系统机制详细讲解

转自&#xff1a;http://www.lanhusoft.com/Article/213.html 请求一个ASP.NET mvc的网站和以前的web form是有区别的&#xff0c;ASP.NET MVC框架内部给我们提供了路由机制&#xff0c;当IIS接受到一个请求时&#xff0c;会先看是否请求了一个静态资源&#xff08;.html,css,…

基于用户 的协同过滤算法

计算用户相似度和用户对未知物品的可能评分 基于用户的协同过滤算法主要包括两个步骤。 (1) 找到和目标用户兴趣相似的用户集合。 (2) 找到这个集合中的用户喜欢的&#xff0c;且目标用户没有听说过的物品推荐给目标用户。 例如现在有A、B、C、D四个用户&#xff0c;分别对a、…

JAVA开源协同过滤算法,推荐算法:协同过滤算法的介绍

一、什么是推荐算法 互联网的出现和普及给用户带来了大量的信息&#xff0c;满足了用户在信息时代对信息的需求&#xff0c;但随着网络的迅速发展而 带来的网上信息量的大幅增长&#xff0c;使得用户在面对大量信息时无法从中获得对自己真正有用的那部分信息&#xff0c;对信息…

协同过滤算法实验

本次呢&#xff0c;简单介绍一下协同过滤算法&#xff0c;并且给出简单的电影推荐实验作为举例说明。利用Spark MLlib中的协同过滤算法完成针对特定用户的电影推荐功能。这一部分需要实现实现输入用户id&#xff0c;输出为其推荐的电影。 一、协同过滤 所谓协同过滤&#xff0…

协同过滤算法及python实现

协同过滤算法及python实现 1.算法简介 协同过滤算法是一种较为著名和常用的推荐算法&#xff0c;它基于对用户历史行为数据的挖掘发现用户的喜好偏向&#xff0c;并预测用户可能喜好的产品进行推荐。也就是常见的“猜你喜欢”&#xff0c;和“购买了该商品的人也喜欢”等功能。…

协同过滤算法理论

1. 协同过滤算法 协同过滤&#xff08;Collaborative Filtering&#xff09;推荐算法是最经典、最常用的推荐算法。 所谓协同过滤&#xff0c;基本思想是根据用户之前的喜好以及其他兴趣相近的用户的选择来给用户推荐物品(基于对用户历史行为数据的挖掘发现用户的喜好偏向&…

协同过滤算法实战

简介 公司给了一个任务&#xff0c;要求根据相似度匹配给教师推荐课程。正好复(预)习一下协同过滤算法。直接探索一下协同过滤应用。 目前教师档案大数据系统中存有海量的教师数据&#xff0c;这些数据对于教师的未来决策&#xff0c;预测教师发展路径&#xff0c;推荐教师课…

协同过滤算法(例题理解)

协同过滤算法是一种推荐系统算法&#xff0c;它利用用户对物品的评价数据来预测用户对未评价物品的喜好程度。该算法基于一个简单的思想&#xff1a;如果两个用户在过去对某些物品的评价很相似&#xff0c;那么在未来他们对这些物品的评价也很可能相似。因此&#xff0c;协同过…

python实现协同过滤算法

协同过滤算法常用于商品推荐或者类似的场合&#xff0c;根据用户之间或商品之间的相似性进行精准推荐 协同过滤算法分为&#xff1a; 基于用户的协同过滤算法&#xff08;UserCF算法&#xff09;&#xff08;适合社交化应用&#xff09;基于商品的协同过滤算法&#xff08;It…

java 协同过滤算法_推荐系统中协同过滤算法实现分析

最近研究Mahout比较多&#xff0c;特别是里面协同过滤算法&#xff1b;于是把协同过滤算法的这个实现思路与数据流程&#xff0c;总结了一下&#xff0c;以便以后对系统做优化时&#xff0c;有个清晰的思路&#xff0c;这样才能知道该如何优化且优化后数据亦能正确。 推荐中的协…

协同过滤算法理解

一、协同过滤思想介绍 顾名思义&#xff0c;协同过滤算法的核心思想就是“物以类聚&#xff0c;人以群分”&#xff0c;通过用户对物品的评价和意见&#xff0c;将物品和人聚成几类&#xff0c;从各自的类中挑选出用户可能感兴趣的的物品进行推荐&#xff0c;而代替从直接从海量…

协同过滤推荐算法的原理及实现

一、协同过滤算法的原理及实现 二、基于物品的协同过滤算法详解 一、协同过滤算法的原理及实现 协同过滤推荐算法是诞生最早&#xff0c;并且较为著名的推荐算法。主要的功能是预测和推荐。算法通过对用户历史行为数据的挖掘发现用户的偏好&#xff0c;基于不同的偏好对用户…

协同过滤算法详解

一、协同过滤算法简介 协同过滤算法是一种较为著名和常用的推荐算法&#xff0c;它基于对用户历史行为数据的挖掘发现用户的喜好偏向&#xff0c;并预测用户可能喜好的产品进行推荐。也就是常见的“猜你喜欢”&#xff0c;和“购买了该商品的人也喜欢”等功能。它的主要实现由…

协同过滤算法

目录 一、什么是协同过滤算法 二、相似度的计算 2.1杰卡德&#xff08;Jaccard&#xff09;相似度 2.2余弦相似度&#xff08;Cosine Similarity&#xff09; 2.3皮尔逊相关系数&#xff08;Pearson Correlation Coefficient&#xff09; 2.4欧氏距离&#xff08;Euclidea…

协同过滤推荐算法

一、协同过滤思想简介 二、协同过滤算法原理介绍 三、基于用户的协同过滤算法描述 四、基于物品的协同过滤算法 基于物品的协同过滤算法的优缺点 一、协同过滤思想简介 协同过滤&#xff0c;从字面上理解&#xff0c;包括协同和过滤两个操作。首先我们在外出和朋友吃饭的时候肯…

推荐系统之协同过滤算法

1、介绍 协同过滤算法&#xff08;Collaborative Filtering&#xff09; 是比较经典常用的推荐算法&#xff0c;从1992年一直延续至今。所谓协同过滤算法&#xff0c;基本思想是根据用户的历史行为数据的挖掘发现用户的兴趣爱好&#xff0c;基于不同的兴趣爱好对用户进行划分并…

oracle数据库中spool的作用,Oracle中Spool命令如何使用 Oracle中Spool命令使用方法

Oracle中Spool命令如何使用&#xff1f;本篇文章小编给大家分享一下Oracle中Spool命令使用方法&#xff0c;小编觉得挺不错的&#xff0c;现在分享给大家供大家参考&#xff0c;有需要的小伙伴们可以来看看。 方法/步骤 首先需要明白Spool是Oracle的命令而不是sql语句。 Spool命…

oracle的Spool命令

使用spool命令实行将sql*plus中的输出的结果复制到一个指定的文件中&#xff0c;或者把查询的结果 发送到打印机中&#xff0c;直接使用spool off命令为止。 spool命令的语法如下&#xff1a; spool [file_name [create|replace|append] off|out] 其中file_name指定一个操作…