由于自己也是才学习Web开发,可能写的东西比较简单,有兴趣的可以看,大牛跟大虾可以帮忙指出问题,可能里面专业术语不会太多,毕竟自己也不懂,还请见谅。
1、直接打开VS2010,创建一个新的MVC框架应用程序。
2、创建完成后,系统基本会将基础工作都做完了,自动生成文件,目录如下:
3、创建数据模型,在Models文件夹处,右击添加“新建项”,设置实体数据模型名称为“NorthWind.edmx”,按照提示一步一步往下,此处我只用一张表来做例子“Categories”。
4、更改控制器,添加Create、Edit、Detail几个方法。
InBlock.gif
namespace MvcApplication1.Controllers
InBlock.gif{
InBlock.gif        [HandleError]
InBlock.gif        
public
class HomeController : Controller
InBlock.gif        {
InBlock.gif                NorthwindEntities northWind =
new NorthwindEntities();
InBlock.gif
                
public ActionResult Index()
InBlock.gif                {
InBlock.gif                        var models = northWind.Categories.ToList();
InBlock.gif
                        
return View(models);
InBlock.gif                }
InBlock.gif
                [AcceptVerbs(HttpVerbs.Get)]
InBlock.gif                
public ActionResult Edit(
int id)
InBlock.gif                {
InBlock.gif                        var models = northWind.Categories.First(c => c.CategoryID == id);
InBlock.gif                        
return View(models);
InBlock.gif                }
InBlock.gif
                [AcceptVerbs(HttpVerbs.Post)]
InBlock.gif                
public ActionResult Edit(
int categoryId, FormCollection form)
InBlock.gif                {
InBlock.gif                        var models = northWind.Categories.First(c => c.CategoryID == categoryId);
InBlock.gif                        UpdateModel(models,
new[] {
"CategoryName",
"Description" });
InBlock.gif                        northWind.SaveChanges();
InBlock.gif
                        
return RedirectToAction(
"Index");
InBlock.gif                }
InBlock.gif
                [AcceptVerbs(HttpVerbs.Get)]
InBlock.gif                
public ActionResult Detail(
int id)
InBlock.gif                {
InBlock.gif                        var models = northWind.Categories.First(c => c.CategoryID == id);
InBlock.gif                        
return View(models);
InBlock.gif                }
InBlock.gif
                [AcceptVerbs(HttpVerbs.Get)]
InBlock.gif                
public ActionResult Create()
InBlock.gif                {
InBlock.gif                        Categories category =
new Categories();
InBlock.gif                        
return View(category);
InBlock.gif                }
InBlock.gif
                [AcceptVerbs(HttpVerbs.Post)]
InBlock.gif                
public ActionResult Create(
int categoryId, FormCollection form)
InBlock.gif                {
InBlock.gif                        var models = northWind.Categories.FirstOrDefault(c => c.CategoryID == categoryId);
InBlock.gif
                        
if (models==
null)
InBlock.gif                        {
InBlock.gif                                Categories category =
new Categories();
InBlock.gif                                UpdateModel(category,
new[] {
"CategoryName",
"Description" });
InBlock.gif                                northWind.AddToCategories(category);
InBlock.gif                                northWind.SaveChanges();
InBlock.gif                                
return RedirectToAction(
"Index");
InBlock.gif
                        }
InBlock.gif                        
else
InBlock.gif                        {
InBlock.gif                                
return RedirectToAction(
"Create");
InBlock.gif                        }                        
InBlock.gif                }
InBlock.gif
                
public ActionResult About()
InBlock.gif                {
InBlock.gif                        
return View();
InBlock.gif                }
InBlock.gif        }
InBlock.gif}
5、创建视图,在相应的控制器处,直接右击,添加视图,各项选择都如下图。这边有个主意的地方就是,我一开始的时候选择强类型视图,怎么选择都是没有Categories,后来通过网上查询,发现是自己没有先编译生成。
6、在视图中,自己也尝试着进行简单的修改,当然不是CSS跟JS这些啦,毕竟还没到那层次。主要还是关键字的显示跟隐藏。
@Html.HiddenFor(model => model.CategoryID)
7、测试了
ValidationMessageFor与
ValidationMessage。
@Html.ValidationMessageFor(model => model.Description)
@Html.ValidationMessage("CategoryName
","*")
这两个的效果大家可以自己测试下。
8、最终效果。
9、遗留问题:
   最后还有2个问题在学习的时候未解决。a、路由:系统自动创建是MapRoute,add的方法我没试过;b、就是在编辑的时候如果我内容出现<>这种字符,就会报错。如果<>连写就是会被自动解析成"
&lt;&gt;
",如果是<字段值>,就会报错"

从客户端(Description="<Test>")中检测到有潜在危险的 Request.Form 值。

"。