MVC 3 基本操作增加修改

在MVC中实现基本的增删改和传统的asp .net 程序有很大的不同,刚开始使用MVC还是有些不太适应,但是它的页面简洁也相当的不同,同时对服务器的访问性能上也有很大的提高。基于此,下面对我学习过程记录如下:

首先,使用VS创建一个以Internet为模板的项目,如下所示:

MVC 3 基本操作增加修改_第1张图片

 

在“_Layout.cshtml”文件中引入相关脚本文件,此文件的路径如下:

MVC 3 基本操作增加修改_第2张图片

 

修改成如下内容:

   1: <head>
   2:     <title>@ViewBag.Titletitle>
   3:     <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="Stylesheet" type="text/css" />
   4:     <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
   5:     <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript">script>
   1:  
   2:     
   1: 
   2:     
script>
   6: head>

在Models文件夹下,新建类文件“Note.cs”,内容如下:

   1: public class Note
   2:     {
   3:         public int Id { get; set; }
   4:         public string Title { get; set; }
   5:         public string Body { get; set; }
   6:     }

 

再创建类文件“NoteManager.cs”,内容如下:

   1: public class NoteManager
   2:     {
   3:         public Collection Notes
   4:         {
   5:             get
   6:             {
   7:                 if (HttpRuntime.Cache["Notes"] == null)
   8:                     loadInitialData(); return (Collection)HttpRuntime.Cache["Notes"];
   9:             }
  10:         }
  11:         private void loadInitialData()
  12:         {
  13:             var notes = new Collection
  14:                             {
  15:                                 new Note
  16:                                     {
  17:                                         Id = 1,
  18:                                         Title = "Set DVR for Sunday",
  19:                                         Body = "Don't forget to record Game of Thrones!"
  20:                                     },
  21:                                 new Note
  22:                                     {Id = 2, Title = "Read MVC article", Body = "Check out the new iwantmymvc.com post"},
  23:                                 new Note
  24:                                     {
  25:                                         Id = 3,
  26:                                         Title = "Pick up kid",
  27:                                         Body = "Daughter out of school at 1:30pm on Thursday. Don't forget!"
  28:                                     },
  29:                                 new Note {Id = 4, Title = "Paint", Body = "Finish the 2nd coat in the bathroom"}
  30:                             };
  31:             HttpRuntime.Cache["Notes"] = notes;
  32:         }        public Collection GetAll() { return Notes; }
  33:         public Note GetById(int id)
  34:         {
  35:             return Notes.Where(i => i.Id == id).FirstOrDefault();
  36:         }
  37:         public int Save(Note item)
  38:         {
  39:             if (item.Id <= 0)
  40:                 return SaveAsNew(item); 
  41:             var existingNote = Notes.Where(i => i.Id == item.Id).FirstOrDefault();
  42:             if(existingNote == null)
  43:             {
  44:                 return -1;
  45:             }
  46:  
  47:             existingNote.Title = item.Title; 
  48:             existingNote.Body = item.Body; 
  49:             return existingNote.Id;
  50:         }
  51:         private int SaveAsNew(Note item)
  52:         {
  53:             item.Id = Notes.Count + 1; Notes.Add(item); return item.Id;
  54:         }
  55:     }

 

修改“HomeController”为如下内容:

   1: public class HomeController : Controller
   2:     {
   3:         public ActionResult Index()
   4:         {
   5:             return View();
   6:         }
   7:         [OutputCache(Duration = 0)]
   8:         public ActionResult List()
   9:         {
  10:             var manager = new NoteManager();
  11:             var model = manager.GetAll();
  12:             return PartialView(model);
  13:         }
  14:         [OutputCache(Duration = 0)]
  15:         public ActionResult Create()
  16:         {
  17:             var model = new Note();
  18:             return PartialView("NoteForm", model);
  19:         }
  20:  
  21:         [OutputCache(Duration = 0)]
  22:         public ActionResult Edit(int id)
  23:         {
  24:             var manager = new NoteManager();
  25:             var model = manager.GetById(id);
  26:             return PartialView("NoteForm", model);
  27:         }
  28:         [HttpPost]
  29:         public JsonResult Save(Note note)
  30:         {
  31:             var manager = new NoteManager();
  32:             var noteId = manager.Save(note);
  33:             return Json(new { Success = noteId > 0 });
  34:         }
  35:     }

对项目编译一下,再新建一个“NoteForm”View,如下图所示:

MVC 3 基本操作增加修改_第3张图片

注意:上图中选择部分视图和使用强类型的View

修改后的内容如下所示:

   1: @model MvcSample.Models.Note
   2:     <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">script>
   1:  
   2:     
   2:  
   3: @using (Html.BeginForm("Save", "Home", FormMethod.Post, new { id = "NoteForm" }))
   4: {
   5:    
   6:  
   7:     
   8:          @Html.ValidationSummary(true, "输入的内容有错,请纠正所有错误,再提交.")
   9:             @Html.HiddenFor(m => m.Id)  
  10:             
class="editor-label">
  11:                 @Html.LabelFor(m => m.Title, "标题")
  12:             
  13:             
class="editor-field">
  14:                 @Html.TextBoxFor(m => m.Title, new { data_val = "true", data_val_required = "标题不能为空。" })
  15:                 @Html.ValidationMessageFor(m => m.Title)
  16:             
  17:  
  18:             
class="editor-label">
  19:                 @Html.LabelFor(m => m.Body, "内容")
  20:             
  21:             
class="editor-field">
  22:                 @Html.TextBoxFor(m => m.Body, new { data_val = "true", data_val_required = "内容不能为空。" })
  23:                 @Html.ValidationMessageFor(m => m.Body)
  24:             
  25:            
  26:     
  27: }
  28:  
  29: 
                    
                    

你可能感兴趣的:(MVC 3 基本操作增加修改)