您目前尚未登陆,请选择【登陆】或【注册
首页->新闻文章->Asp.net3.5 MVC入门之文章管理源码>>MvcArticleDemo/Controllers/ArticleController.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Asp.net3.5 MVC入门之文章管理源码
普通视图
		            
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Web; 5using System.Web.Mvc; 6using MvcArticleDemo.Models; 7using System.Web.Routing; 8//该源码首发自www.51aspx.com(51aspx.com) 9 10namespace MvcArticleDemo.Controllers 11{ 12 public class ArticleController : Controller 13 { 14 15 public ActionResult NewCategory() 16 { 17 if (TempData.ContainsKey("ErrorMessage")) 18 { 19 foreach (var item in TempData) 20 { 21 ViewData[item.Key] = item.Value; 22 } 23 } 24 return View("NewCategory",ViewData); 25 } 26 27 public object NewNews() 28 { 29 if (TempData.ContainsKey("ErrorMessage")) 30 { 31 foreach (var item in TempData) 32 { 33 ViewData[item.Key] = item.Value; 34 } 35 } 36 Models.ArticlesNewViewData viewData = new ArticlesNewViewData(); 37 Models.ArticleDemoDataContext articleDc = new MvcArticleDemo.Models.ArticleDemoDataContext(); 38 // Models.Categories category = articleDc.Categories.SingleOrDefault(c => c.ID == id); 39 viewData.categories = articleDc.Categories.ToList(); 40 // ViewData["CategoryName"] = new SelectList(articleDc.Categories.ToList(), "ID", "CategoryName", ViewData["ID"] ?? category.ID); 41 ViewData["categoryid"] = new SelectList(viewData.categories, "ID", "CategoryName"); 42 return View("NewNews", viewData); 43 } 44 45 public object CreateNews() 46 { 47 Models.Article article = new Article(); 48 Models.Categories category = new MvcArticleDemo.Models.Categories(); 49 Models.ArticleDemoDataContext articleDc = new MvcArticleDemo.Models.ArticleDemoDataContext(); 50 51 if (!Valid("ArticleName")) 52 { 53 Request.Form.CopyTo(TempData); 54 TempData["ErrorMessage"] = "请填写文章标题"; 55 56 return RedirectToAction("NewNews"); 57 } 58 if (!Valid("categoryid")) 59 { 60 Request.Form.CopyTo(TempData); 61 TempData["ErrorMessage"] = "请选择新闻类别"; 62 63 return RedirectToAction("NewNews"); 64 } 65 66 if (!Valid("ArticleContent")) 67 { 68 Request.Form.CopyTo(TempData); 69 TempData["ErrorMessage"] = "请填写文章内容"; 70 71 return RedirectToAction("NewNews"); 72 } 73 74 BindingHelperExtensions.UpdateFrom(article, Request.Form); 75 article.Date = DateTime.Today; 76 articleDc.Article.InsertOnSubmit(article); 77 articleDc.SubmitChanges(); 78 79 return RedirectToRoute(new RouteValueDictionary(new { Controller = "Article", Action = "List", ID =article.Categories.CategoryName })); 80 } 81 public ActionResult Edit(int id) 82 { 83 if (TempData.ContainsKey("ErrorMessage")) 84 { 85 foreach (var item in TempData) 86 { 87 ViewData[item.Key] = item.Value; 88 } 89 } 90 Models.ArticleDemoDataContext articleDc = new MvcArticleDemo.Models.ArticleDemoDataContext(); 91 Models.Categories category = articleDc.Categories.SingleOrDefault(c => c.ID == id); 92 //ViewData["CategoryName"] = new SelectList(articleDc.Categories.ToList(), "ID", "CategoryName", ViewData["ID"] ?? category.ID); 93 //ViewData["CategoryName"] = new SelectList(articleDc.Categories.ToList(), "CategoryID", "CategoryName", ViewData["CategoryID"] ?? category.ID); 94 return View("EditCategory", category); 95 96 } 97 98 public object Update(int id) 99 { 100 Models.ArticleDemoDataContext articleDc = new MvcArticleDemo.Models.ArticleDemoDataContext(); 101 Models.Categories category = articleDc.Categories.SingleOrDefault(c => c.ID == id); 102 if (!Valid("CategoryName")) 103 { 104 Request.Form.CopyTo(TempData); 105 TempData["ErrorMessage"] = "请填写新闻名称"; 106 107 return RedirectToAction("Edit", new { ID=id}); 108 } 109 BindingHelperExtensions.UpdateFrom(category, Request.Form); 110 111 articleDc.SubmitChanges(); 112 113 return RedirectToRoute(new RouteValueDictionary(new { Controller = "Home", Action = "Index", ID = category.CategoryName })); 114 115 } 116 public object Delete(int id) 117 { 118 Models.ArticleDemoDataContext articleDc = new MvcArticleDemo.Models.ArticleDemoDataContext(); 119 Models.Categories category = articleDc.Categories.SingleOrDefault(c => c.ID == id); 120 articleDc.Categories.DeleteOnSubmit(category); 121 articleDc.SubmitChanges(); 122 return RedirectToRoute(new RouteValueDictionary(new { Controller = "Home", Action = "Index" })); 123 124 } 125 public object Create() 126 { 127 Models.Categories category = new MvcArticleDemo.Models.Categories(); 128 Models.ArticleDemoDataContext articleDc = new MvcArticleDemo.Models.ArticleDemoDataContext(); 129 if (!Valid("CategoryName")) 130 { 131 Request.Form.CopyTo(TempData); 132 TempData["ErrorMessage"] = "请填写新闻名称"; 133 134 return RedirectToAction("NewCategory"); 135 } 136 Models.Categories cate = articleDc.Categories.SingleOrDefault(c => c.CategoryName==Request.Form["categoryname"]); 137 if (null != cate) 138 { 139 Request.Form.CopyTo(TempData); 140 TempData["ErrorMessage"] = "新闻类别名称不能重复"; 141 142 return RedirectToAction("NewCategory"); 143 } 144 BindingHelperExtensions.UpdateFrom(category, Request.Form); 145 articleDc.Categories.InsertOnSubmit(category); 146 articleDc.SubmitChanges(); 147 148 return RedirectToRoute(new RouteValueDictionary(new { Controller="Home",Action = "Index", ID = category.CategoryName })); 149 } 150 151 public object List(string id) 152 { 153 Models.Categories category = new MvcArticleDemo.Models.Categories(); 154 // Models.Article article = new Article(); 155 Models.ArticleDemoDataContext articleDc = new MvcArticleDemo.Models.ArticleDemoDataContext(); 156 var categories = articleDc.Categories.SingleOrDefault(c => c.CategoryName == id); 157 158 var article = from p in articleDc.Article 159 where p.CategoryID == categories.ID 160 select p; 161 162 // ViewData["Title"] = "Hello World!"; 163 ViewData["CategoryName"] = id; 164 165 //this.ViewEngine = new MvcContrib.NHamlViewEngine.NHamlViewFactory(); 166 return View("ArticleList", article.ToList()); 167 } 168 bool Valid(string controlName) 169 { 170 bool isValid = true; 171 if (string.IsNullOrEmpty(Request.Form[controlName])) 172 isValid = false; 173 TempData["Error:" + controlName] = Request.Form[controlName]; 174 return isValid; 175 } 176 177 public object Detail(int id) 178 { 179 Models.ArticleDemoDataContext articleDc = new MvcArticleDemo.Models.ArticleDemoDataContext(); 180 Models.Article article = articleDc.Article.SingleOrDefault(a => a.ID == id); 181 return View(article); 182 } 183 } 184} 185
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:Asp.net3.5 MVC入门之文章管理源码
51Aspx.com 版权所有 CopyRight © 2000-2008. 京ICP备06046876号