本篇内容主要讲解“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。