Asp.NetMVC4.0如何开发栏目内容模型管理
本篇内容主要讲解“Asp.Net MVC4.0如何开发栏目内容模型管理”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Asp.Net MVC4.0如何开发栏目内容模型管理”吧!

成都创新互联主要从事做网站、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务青原,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
网站后台中有栏目管理功能,对栏目管理中有栏目内容模块项,比如我们把网站的每个栏目划分为新闻模块、图片模块、文章模块、产品模块等。
栏目的模型类别管理包括模块名称、模块类型、是否启用、描述等。
一、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 DbSetcategoryModel { 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
{
private HillstoneContext db = new HillstoneContext();
///
/// 读取模块列表
///
///
public IQueryable List()
{
var _categoryModel = db.categoryModel;
return _categoryModel;
}
///
/// 读取一条模块数据
///
///
///
public override SysCategoryModel Find(int Id)
{
var _categoryModel = db.categoryModel.SingleOrDefault(c => c.ModelID == Id);
return _categoryModel;
}
///
/// 添加一条模块数据
///
///
///
public override bool Add(SysCategoryModel Tmodel)
{
db.categoryModel.Add(Tmodel);
if (db.SaveChanges() > 0)
{
return true;
}
else {
return false;
}
}
///
/// 删除一条模块数据
///
/// ID
///
public override bool Delete(int Id)
{
var _categoryModel = db.categoryModel.FirstOrDefault(c => c.ModelID == Id);
if (_categoryModel != null)
{
return Delete(_categoryModel);
}
else {
return false;
}
}
///
/// 删除一条模块数据
///
/// 数据模型
///
public bool Delete( SysCategoryModel sysCategoryModel)
{
db.categoryModel.Remove(sysCategoryModel);
if (db.SaveChanges() > 0)
{
return true;
}
else
{
return false;
}
}
///
/// 修改模块数据
///
/// 模块数据模型
///
public override bool Update(SysCategoryModel Tmodel)
{
db.categoryModel.Attach(Tmodel);
db.Entry(Tmodel).State = System.Data.Entity.EntityState.Modified;
if(db.SaveChanges()>0){
return true;
}
else
{
return false;
}
}
}
} 注意:在编辑的时候要引用
using System.Data.Entity;
才能使用
db.Entry(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();
///
/// 列表页面
///
///
public ActionResult List()
{
var _categoryModel = categoryModelRsy.List();
return View(_categoryModel);
}
///
/// 添加页面
///
///
public ActionResult Create()
{
return View();
}
///
/// 提交添加页面
///
///
///
[HttpPost]
public ActionResult Create(SysCategoryModel sysCategoryModel)
{
if (ModelState.IsValid)
{
if (categoryModelRsy.Add(sysCategoryModel))
{
ModelState.AddModelError("Message", "成功!");
}
else
{
ModelState.AddModelError("Message", "失败!");
}
}
return View();
}
///
/// 编辑页面
///
///
public ActionResult Edit(int id)
{
var categoryModel = categoryModelRsy.Find(id);
return View(categoryModel);
}
///
/// 提交编辑页面
///
///
///
[HttpPost]
public ActionResult Edit(SysCategoryModel sysCategoryModel)
{
if (ModelState.IsValid) {
if (categoryModelRsy.Update(sysCategoryModel))
{
ModelState.AddModelError("Message", "成功!");
}
else {
ModelState.AddModelError("Message","失败!");
}
}
return View();
}
///
/// 查看页面
///
///
///
public ActionResult Details(int id)
{
var _sysCategoryModel = categoryModelRsy.Find(id);
return View(_sysCategoryModel);
}
///
/// 删除一条模块
///
/// ID
///
[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@{ ViewBag.Title = "List"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section Scripts{ @Scripts.Render("~/bundles/jqueryval") } List
@Html.ActionLink("Create New", "Create")
@foreach (var item in Model) { @Html.DisplayNameFor(model => model.ModelName) @Html.DisplayNameFor(model => model.ModelType) @Html.DisplayNameFor(model => model.Enable) @Html.DisplayNameFor(model => model.Description) } @Html.DisplayFor(modelItem => item.ModelName) @Html.DisplayFor(modelItem => item.ModelType) @Html.DisplayFor(modelItem => item.Enable) @Html.DisplayFor(modelItem => item.Description) @Html.ActionLink("编辑", "Edit", new { id=item.ModelID }) @Html.ActionLink("查看", "Details", new { id=item.ModelID }) | 删除
其他页面基本保持不变。
到此,相信大家对“Asp.Net MVC4.0如何开发栏目内容模型管理”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
文章名称:Asp.NetMVC4.0如何开发栏目内容模型管理
分享URL:http://www.jxjierui.cn/article/ggccch.html


咨询
建站咨询
