MVC框架其实关注了很久,一直没时间去研究,老感觉比较深奥的.加上现在算是找到份开发的工作,利用国庆长假期间,研究了一下.虽然是些最基本的东西,不过却是开始呢。或者以后开发,思路也会出现转变,传统的ASP.net开发到现在的MVC框架。
好了,废话少说,看如何建立MVC项目。
首先,建立MVC项目,在WEB里面可以选择,前提是安装了VS2008插件,建立好工程的架构如下
项目架构如左图所示,Models是模型层,Views是视图层,controllers是控制层。就是所谓的MVC了。和传统的MVC模式(三层架构)是有区别的。
Models层:这里我用了ADO.net Entity data model,也可以用Linq to sql等其他ORM框架
Controllers层:默认微软送了2个基本的Controller你,名称后面的不能改,例如多建个CatalogController
Controllers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
GrandEntities db = new GrandEntities();
public ActionResult Index()
{
var model = db.T_business_catalog.Where(c => c.MeVer.Equals("en")).ToList();
return View(model);
}
public ActionResult About()
{
return View();
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Details(int id)
{
var model = db.T_business_catalog.First(c => c.CatalogID == id);
return View(model);
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Create()
{
T_business_catalog catalog = new T_business_catalog();
return View(catalog);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int catalogId, FormCollection form)
{
var model = db.T_business_catalog.FirstOrDefault(c => c.CatalogID == catalogId);
if (model == null)
{
T_business_catalog catalog = new T_business_catalog();
UpdateModel(catalog);
db.AddToT_business_catalog(catalog);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
return RedirectToAction("Create");
}
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Edit(int id)
{
var model = db.T_business_catalog.First(c => c.CatalogID == id);
IList<T_business_catalog> catalist = db.T_business_catalog.Distinct().ToList();
ViewData["MeVerName"] = new SelectList(catalist, "MeVer", "MeVer");
return View(model);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection form)
{
var model = db.T_business_catalog.First(c => c.CatalogID == id);
UpdateModel(model, new string[] { "CatalogName" });
return RedirectToAction("Index");
}
}
} 随便例举了一个controllers的写法,这里查找和操作数据库,基本用了LINQ了,感觉这个MVC框架和ORM组合挺不错的。增删,改查基本的方法都出来了。注意,他返回的是ActionResult,也就是数据处理后直接返回到Views层。所以Views层只是作为显示数据的地方,甚至可以把View层看作是静态的页面,不处理任何逻辑关系的。所以Views层新架构中,没有了后台的CS代码。
Views层:先看如何建立,可以是
,在Views文件夹右键,添加。
或者是
在controllers层对应的方法右键,会看到Add View,然后选择对应的:
到此,运行就可以看到MVC框架做出来的基本模型了。
她给我的第一印象是简洁,没有多余的代码。下面的页面代码,甚至基本是自动生成的。
Code
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.T_business_catalog>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Index</h2>
<table>
<tr>
<%--<th>
CatalogID
</th>--%>
<th>
CatalogName
</th>
<%-- <th>
CatalogTable
</th>--%>
<th>
MeVer
</th>
<th>
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<%-- <td>
<%= Html.Encode(item.CatalogID) %>
</td>--%>
<td>
<%= Html.Encode(item.CatalogName) %>
</td>
<%--<td>
<%= Html.Encode(item.CatalogTable) %>
</td>--%>
<td>
<%= Html.Encode(item.MeVer) %>
</td>
<td>
<%= Html.ActionLink("Edit", "Edit", new { id=item.CatalogID }) %>
|
<%= Html.ActionLink("Details", "Details", new { id=item.CatalogID })%>
</td>
</tr>
<% } %>
</table>
<p>
<%= Html.ActionLink("Create New", "Create") %>
</p>
</asp:Content>
出来的效果也蛮快的。
好了。MVC就是这么简单,当然,要做复杂也可以做更加大的项目的,传统与创新,你要如何选择呢?
下个项目要把MVC框架结合ORM,结合JQUERY等。