MVC2实例
时间:2010-10-20 来源:小绿虫
选择C#应用程序,在web应用程序中没有ADO.NET实体数据模型
点击下一步
新建sql连接
只选择一个表就够了:Categories表
实体类创建成功
2.控制器 Controllers
先删除该文件夹下的HomeController
然后在Controllers文件夹上单击鼠标右键 添加控制器
选择增删查该操作方法
3.修改控制器代码:
先看看刚刚创建好的实体类的命名空间,Northwind.Disigner.cs
namespace MvcApplication2.Models{
#region 上下文
/// <summary>
/// 没有元数据文档可用。
/// </summary>
public partial class NorthwindEntities : ObjectContext
在home控制器下添加实体类的实例化代码
public class HomeController : Controller{
NorthwindEntities Northwind = new NorthwindEntities();//实例化实体类 修改添加方法
代码 [AcceptVerbs(HttpVerbs.Get)]
public ActionResult Create()
{
Categories cate = new Categories();
return View(cate);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int categoryId,FormCollection form)
{
var model = Northwind.Categories.First(c => c.CategoryID == categoryId);
if (model == null)
{
Categories category = new Categories();
UpdateModel(model, new[] { "CategoryName", "Description" });
Northwind.AddToCategories(category);
Northwind.SaveChanges();
return RedirectToAction("Index");
}
else
{
return RedirectToAction("Create");
}
}
[AcceptVerbs(HttpVerbs.Post)] 提交表单的一个标记 ,以post方式提交
修改详细信息代码:
public ActionResult Details(int id){
var model = Northwind.Categories.First(c => c.CategoryID == id);
return View(model);
} 修改编辑代码: 代码 public ActionResult Edit(int id)
{
var model = Northwind.Categories.First(c => c.CategoryID == id);
return View(model);
}
//[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
var model = Northwind.Categories.First(c => c.CategoryID == id);
UpdateModel(model, new[] { "CategoryName", "Description" });
Northwind.SaveChanges();
return RedirectToAction("Index");
}
暂时不做删除!
控制器完整代码:
代码 using System;using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class HomeController : Controller
{
NorthwindEntities Northwind = new NorthwindEntities();//实例化实体类
public ActionResult Index()
{
var model = Northwind.Categories.ToList();
return View(model);
}
public ActionResult Details(int id)
{
var model = Northwind.Categories.First(c => c.CategoryID == id);
return View(model);
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Create()
{
Categories cate = new Categories();
return View(cate);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int categoryId,FormCollection form)
{
var model = Northwind.Categories.First(c => c.CategoryID == categoryId);
if (model == null)
{
Categories category = new Categories();
UpdateModel(model, new[] { "CategoryName", "Description" });
Northwind.AddToCategories(category);
Northwind.SaveChanges();
return RedirectToAction("Index");
}
else
{
return RedirectToAction("Create");
}
}
public ActionResult Edit(int id)
{
var model = Northwind.Categories.First(c => c.CategoryID == id);
return View(model);
}
//[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
var model = Northwind.Categories.First(c => c.CategoryID == id);
UpdateModel(model, new[] { "CategoryName", "Description" });
Northwind.SaveChanges();
return RedirectToAction("Index");
}
public ActionResult Delete(int id)
{
return View();
}
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
4.创建view视图
在Index或其他代码的方法上点鼠标右键 添加视图
选择创建强类型视图,并选择视图数据类: Index对应List,其他他方法名对应相应的视图内容,Create、Details、Eidt
功能效果:
八卦:以后MVC主要围绕 ASP.NET3.5,会使用到 lambda,linq,实体类框架(entity framework),有可能还会使用到如AOP,NHibernate,先搭建一个示例,其他知识后面详细介绍。
学习方法,首先给自己一个信心,信心来自哪里?有一个能看得到的UI效果,这样才能不断督促我们去学习和了解它是如何来的。
源码:/Files/Simcoder/MvcApplication2.rar