Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(八)Putting It All Together
来源:互联网 发布:代理软件有哪些 编辑:程序博客网 时间:2024/05/19 02:18
截止到目前为止,我们已经介绍了组成asp.net mvc应用程序的所有部分,接下来我们put it all together!
添加controller
因为controller就是一个实现了asp.net mvc controller接口的一个普通的类,所以,我们可以手动在controller文件夹下添加一个类,然后继承system.web.mvc.controller,然后添加action。当然,VS为我们提供了一些工具让我们更加方便地添加controller,右键controller文件夹——添加——controller
接下来,我们修改detail action,在里面生成一个Auction的实例,然后把引用通过 View(object model)方法传到view。如果正常情况下,我们获取信息是从数据库中,在这里我们就先手动的写上去。
// // GET: /Auctions/Details/5 public ActionResult Details(int id) { var auction = new Models.Auction { Id = id, Title = "Brand new Widget 2.0", Description = "This is a brand new version 2.0 Widget!", StartPrice = 1.00m, CurrentPrice = 13.40m, StartTime = DateTime.Parse("2012-01-01"), EndTime = DateTime.Parse("6-23-2012 12:34 PM") }; return View(auction); }添加view
当然我们也可以像添加controller那样,手动地添加view,我们也可以利用VS的自动工具来生成view。
点击action单击添加视图,我们就会弹出下面的框:
如果我们选择了模型类,我们还可以选择相应的支架模板,在这里我们选择details
注:如果我们在模型类的下拉列表里没有找到我们要的类,那么我们只要编译一下就可了!
当我们单击添加按钮,我们可以看到VS已经分析了model,生成了需要的html代码,也添加了对html helpers的引用,比如用html.displayFor,显示Auction的字段。
@model InternetEBuy4.Models.Auction@{ ViewBag.Title = "Details";}<h2>Details</h2><fieldset> <legend>Auction</legend> <div class="display-label"> @Html.DisplayNameFor(model => model.Title) </div> <div class="display-field"> @Html.DisplayFor(model => model.Title) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.Description) </div> <div class="display-field"> @Html.DisplayFor(model => model.Description) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.StartPrice) </div> <div class="display-field"> @Html.DisplayFor(model => model.StartPrice) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.CurrentPrice) </div> <div class="display-field"> @Html.DisplayFor(model => model.CurrentPrice) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.StartTime) </div> <div class="display-field"> @Html.DisplayFor(model => model.StartTime) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.EndTime) </div> <div class="display-field"> @Html.DisplayFor(model => model.EndTime) </div></fieldset><p> @Html.ActionLink("Edit", "Edit", new { id=Model.Id }) | @Html.ActionLink("Back to List", "Index")</p>接下来,我们来看一下运行情况:
在用Internet模板生成的代码里,是默认添加了身份验证的,如果我们在上面的action上添加如下代码:
[Authorize]
那么我们再访问上面的页面,就会自动跳转到登录页面,详细的讨论我会在后续博文中提到。
- Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(八)Putting It All Together
- Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(一)
- Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(二)
- Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(三)Convention over Configuration
- Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(四)Routing
- Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(五)Controller
- Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(六)View
- Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(七)Models
- Putting It All Together
- Programming ASP.NET MVC 4 - Chap01 - ASP.NET MVC 基础
- ASP.NET WebForms and MVC together in one project
- Asp.net Mvc Framework 八 (Helper)
- Asp.net Mvc Framework 八 (Helper)
- ASP.NET MVC 八 DropDownList数据绑定
- asp.net mvc 3 [USE IT!]
- ASP.NET MVC 框架
- ASP.NET MVC 框架
- ASP.NET MVC
- C++学习笔记2
- 一些Mac OS X的使用技巧
- C语言文件操作
- 获取手机的详细信息
- C++ COM 实现类厂
- Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(八)Putting It All Together
- C++ COM添加接口
- c++中类的默认构造函数,析构函数,拷贝构造函数
- Overlay
- 802.11 学习笔记
- VB.NET中的多窗体编程:升级到 .NET
- Android 的 openFileOutput()
- fb_info结构体定义原型
- linux系统配置informatica odbc