您好,登錄后才能下訂單哦!
本篇內容主要講解“Asp.Net MVC4.0如何開發欄目內容模型管理”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Asp.Net MVC4.0如何開發欄目內容模型管理”吧!
網站后臺中有欄目管理功能,對欄目管理中有欄目內容模塊項,比如我們把網站的每個欄目劃分為新聞模塊、圖片模塊、文章模塊、產品模塊等。
欄目的模型類別管理包括模塊名稱、模塊類型、是否啟用、描述等。
一、Models屬性:
using System.Linq; using System.Web; namespace Hillstone.Models { public class SysCategoryModel { [Key] public int ModelID { get; set; } [Display(Name="模型名稱")] [Required(ErrorMessage="×")] [StringLength(50,ErrorMessage="×")] public string ModelName { get; set; } [Display(Name = "模型類型")] [Required(ErrorMessage = "×")] [StringLength(50, ErrorMessage = "×")] public string ModelType { get; set; } [Display(Name="是否啟用")] [Required(ErrorMessage = "×")] public bool Enable{ get; set; } [Display(Name="模型描述")] [StringLength(100,ErrorMessage="×")] public string Description{ get; set; } } }
是否啟用:如果不啟用,那么在此類型的欄目將不能使用。
二、DAL數據層:訪問或設置實體數據集
public DbSet<SysCategoryModel> categoryModel { get; set; }
三、BLL業務邏輯層:讀取、新增、編輯、刪除、查看功能
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Hillstone.Models; using System.Data.Entity; using Hillstone.DAL; namespace Hillstone.BLL { public class SysCategoryModelReponsitory : IRepositoryBase<SysCategoryModel> { private HillstoneContext db = new HillstoneContext(); /// <summary> /// 讀取模塊列表 /// </summary> /// <returns></returns> public IQueryable<SysCategoryModel> List() { var _categoryModel = db.categoryModel; return _categoryModel; } /// <summary> /// 讀取一條模塊數據 /// </summary> /// <param name="Id"></param> /// <returns></returns> public override SysCategoryModel Find(int Id) { var _categoryModel = db.categoryModel.SingleOrDefault(c => c.ModelID == Id); return _categoryModel; } /// <summary> /// 添加一條模塊數據 /// </summary> /// <param name="Tmodel"></param> /// <returns></returns> public override bool Add(SysCategoryModel Tmodel) { db.categoryModel.Add(Tmodel); if (db.SaveChanges() > 0) { return true; } else { return false; } } /// <summary> /// 刪除一條模塊數據 /// </summary> /// <param name="Id">ID</param> /// <returns></returns> public override bool Delete(int Id) { var _categoryModel = db.categoryModel.FirstOrDefault(c => c.ModelID == Id); if (_categoryModel != null) { return Delete(_categoryModel); } else { return false; } } /// <summary> /// 刪除一條模塊數據 /// </summary> /// <param name="sysCategoryModel">數據模型</param> /// <returns></returns> public bool Delete( SysCategoryModel sysCategoryModel) { db.categoryModel.Remove(sysCategoryModel); if (db.SaveChanges() > 0) { return true; } else { return false; } } /// <summary> /// 修改模塊數據 /// </summary> /// <param name="Tmodel">模塊數據模型</param> /// <returns></returns> public override bool Update(SysCategoryModel Tmodel) { db.categoryModel.Attach(Tmodel); db.Entry<SysCategoryModel>(Tmodel).State = System.Data.Entity.EntityState.Modified; if(db.SaveChanges()>0){ return true; } else { return false; } } } }
注意:在編輯的時候要引用
using System.Data.Entity;
才能使用
db.Entry<SysCategoryModel>(Tmodel).State = System.Data.Entity.EntityState.Modified;
四、控制器:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Hillstone.BLL; using Hillstone.Models; namespace Hillstone.Controllers.Sys.Cotegory { public class CategoryModelController : Controller { private SysCategoryModelReponsitory categoryModelRsy = new SysCategoryModelReponsitory(); /// <summary> /// 列表頁面 /// </summary> /// <returns></returns> public ActionResult List() { var _categoryModel = categoryModelRsy.List(); return View(_categoryModel); } /// <summary> /// 添加頁面 /// </summary> /// <returns></returns> public ActionResult Create() { return View(); } /// <summary> /// 提交添加頁面 /// </summary> /// <param name="sysCategoryModel"></param> /// <returns></returns> [HttpPost] public ActionResult Create(SysCategoryModel sysCategoryModel) { if (ModelState.IsValid) { if (categoryModelRsy.Add(sysCategoryModel)) { ModelState.AddModelError("Message", "成功!"); } else { ModelState.AddModelError("Message", "失敗!"); } } return View(); } /// <summary> /// 編輯頁面 /// </summary> /// <returns></returns> public ActionResult Edit(int id) { var categoryModel = categoryModelRsy.Find(id); return View(categoryModel); } /// <summary> /// 提交編輯頁面 /// </summary> /// <param name="sysCategoryModel"></param> /// <returns></returns> [HttpPost] public ActionResult Edit(SysCategoryModel sysCategoryModel) { if (ModelState.IsValid) { if (categoryModelRsy.Update(sysCategoryModel)) { ModelState.AddModelError("Message", "成功!"); } else { ModelState.AddModelError("Message","失敗!"); } } return View(); } /// <summary> /// 查看頁面 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult Details(int id) { var _sysCategoryModel = categoryModelRsy.Find(id); return View(_sysCategoryModel); } /// <summary> /// 刪除一條模塊 /// </summary> /// <param name="id">ID</param> /// <returns></returns> [HttpPost] public ActionResult Delete(int id) { if (categoryModelRsy.Delete(id)) { return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK); } else { return new HttpStatusCodeResult(System.Net.HttpStatusCode.NotFound); } } //AJAX刪除功能 [HttpPost] public JsonResult DeleteAsJson(int id) { var _categoryModel = categoryModelRsy.Find(id); if (categoryModelRsy != null) { return Json(categoryModelRsy.Delete(id); } else { return Json(false); } } } }
五、視圖頁面
1、顯示列表:主要是Json刪除功能的實現
@model IEnumerable<Hillstone.Models.SysCategoryModel> @{ ViewBag.Title = "List"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section Scripts{ @Scripts.Render("~/bundles/jqueryval") <script type="text/javascript"> function DeleteAsJson(id,name) { if (confirm("你確認要刪除" + name + "?")) { $.post("@Url.Action("DeleteAsJson","CategoryModel")", { Id: id }, function (data) { if (data) { alert("刪除成功!"); location.reload(); } }) } } @* $("#btn_del").click(function () { if (confirm("您確定要刪除改欄目嗎?\n如該欄目有子欄目請先刪除子欄目!")) { $.post("@Url.Action("ManageDeleteJson","Category")", { id: $("#CategoryId").val() }, function (data) { if (data) { alert("成功刪除欄目!"); top.location = "@Url.Action("Manage", "Category")"; } else { alert("刪除欄目失敗!如該欄目有子欄目請先刪除子欄目。"); } }); } });*@ </script> } <h3>List</h3> <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr> <th> @Html.DisplayNameFor(model => model.ModelName) </th> <th> @Html.DisplayNameFor(model => model.ModelType) </th> <th> @Html.DisplayNameFor(model => model.Enable) </th> <th> @Html.DisplayNameFor(model => model.Description) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.ModelName) </td> <td> @Html.DisplayFor(modelItem => item.ModelType) </td> <td> @Html.DisplayFor(modelItem => item.Enable) </td> <td> @Html.DisplayFor(modelItem => item.Description) </td> <td> @Html.ActionLink("編輯", "Edit", new { id=item.ModelID }) @Html.ActionLink("查看", "Details", new { id=item.ModelID }) | <a id="btn_del" href="javascript:void(0)" onclick="DeleteAsJson(@item.ModelID,'@item.ModelName')">刪除</a> </td> </tr> } </table>
其他頁面基本保持不變。
到此,相信大家對“Asp.Net MVC4.0如何開發欄目內容模型管理”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。