In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to develop column content model management in Asp.Net MVC4.0". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Asp.Net MVC4.0 how to develop column content model management" bar!
There is a column management function in the background of the website, and there are column content module items in the column management. for example, we divide each column of the website into news module, picture module, article module, product module and so on.
The model category management of the column includes module name, module type, whether it is enabled, description and so on.
1. Models attribute:
Using System.Linq;using System.Web;namespace Hillstone.Models {public class SysCategoryModel {[Key] public int ModelID {get; set;} [Display (Name= "Model name")] [Required (ErrorMessage= "×")] [StringLength (50Menery ErrorMessage = "×")] public string ModelName {get; set } [Display (Name= "Model Type")] [Required (ErrorMessage = "×")] [StringLength (50, ErrorMessage = "×")] public string ModelType {get; set;} [Display (whether Name= "is enabled")] [Required (ErrorMessage = "×")] public bool Enable {get; set } [Display (Name= "Model description")] [StringLength (100m ErrorMessage = "×")] public string Description {get; set;}}
Whether or not enabled: if not enabled, columns of this type will not be available.
II. DAL data layer: access or set physical datasets
Public DbSet categoryModel {get; set;}
Third, BLL business logic layer: read, add, edit, delete, view functions
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 () / read module list / public IQueryable List () {var _ categoryModel = db.categoryModel; return _ categoryModel } / read a piece of module data / public override SysCategoryModel Find (int Id) {var _ categoryModel = db.categoryModel.SingleOrDefault (c = > c.ModelID = = Id); return _ categoryModel } / add a module data / 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 }} / delete a module data / data model / public bool Delete (SysCategoryModel sysCategoryModel) {db.categoryModel.Remove (sysCategoryModel) If (db.SaveChanges () > 0) {return true;} else {return false }} / modify module data / 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;}
Note: quote when editing
Using System.Data.Entity
To use it.
Db.Entry (Tmodel). State = System.Data.Entity.EntityState.Modified
4. Controller:
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 () / list page / public ActionResult List () {var _ categoryModel = categoryModelRsy.List (); return View (_ categoryModel) } / add page / public ActionResult Create () {return View () } / submit add page / [HttpPost] public ActionResult Create (SysCategoryModel sysCategoryModel) {if (ModelState.IsValid) {if (categoryModelRsy.Add (sysCategoryModel)) { ModelState.AddModelError ("Message" "success!") } else {ModelState.AddModelError ("Message", "failure!");}} return View () } / Edit page / public ActionResult Edit (int id) {var categoryModel = categoryModelRsy.Find (id); return View (categoryModel) } / submit editing page / [HttpPost] public ActionResult Edit (SysCategoryModel sysCategoryModel) {if (ModelState.IsValid) {if (categoryModelRsy.Update (sysCategoryModel)) {ModelState.AddModelError ("Message") "success!") Else {ModelState.AddModelError ("Message", "failure!") ;}} return View ();} / View the page / public ActionResult Details (int id) {var _ sysCategoryModel = categoryModelRsy.Find (id); return View (_ sysCategoryModel) Delete a module / 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 deletion function [HttpPost] public JsonResult DeleteAsJson (int id) {var _ categoryModel = categoryModelRsy.Find (id) If (categoryModelRsy! = null) {return Json (categoryModelRsy.Delete (id);} else {return Json (false);}
5. View page
1. Display list: mainly the realization of Json deletion function
@ model IEnumerable@ {ViewBag.Title = "List"; Layout = "~ / Views/Shared/_Layout.cshtml" } @ section Scripts {@ Scripts.Render ("~ / bundles/jqueryval") function DeleteAsJson (id,name) {if (confirm ("are you sure you want to delete" + name + "?")) {$.post ("@ Url.Action (" DeleteAsJson "," CategoryModel ")", {Id: id} Function (data) {if (data) {alert ("deleted successfully!") Location.reload ();} @ * $("# btn_del") .click (function () {if (confirm) ("are you sure you want to delete the change column? \ nPlease delete the subcolumn first if there is a subcolumn in this column! ") {$.post ("@ Url.Action (" ManageDeleteJson "," Category ")", {id: $("# CategoryId") .val ()}, function (data) {if (data) {alert ("column deleted successfully!") ; top.location = "@ Url.Action (" Manage "," Category ")";} else {alert ("failed to delete the column! please delete the subcolumn first if there is a subcolumn in the column.") ;}});}}); * @} List
@ Html.ActionLink ("Create New", "Create")
Html.DisplayNameFor (model = > model.ModelName) @ Html.DisplayNameFor (model = > model.ModelType) @ Html.DisplayNameFor (model = > model.Enable) @ Html.DisplayNameFor (model = > model.Description) @ foreach (var item in Model) {@ Html .DisplayFor (modelItem = > item.ModelName) @ Html.DisplayFor (modelItem = > item.ModelType) @ Html.DisplayFor (modelItem = > item.Enable) @ Html.DisplayFor (modelItem = > item.Description) @ Html.ActionLink ("Edit" "Edit", new {id=item.ModelID}) @ Html.ActionLink ("View", "Details", new {id=item.ModelID}) |
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.