Preface:
Although. net mvc provides great convenience for me, if there are more functional blocks, It is a headache to add, delete, modify, query, and write these functions.
So I figured out a way
By inheriting the addition, deletion, modification, and query of Public modules, we can achieve our own lazy thinking.
Now I give some common public code
I hope you will discuss whether the implementation of this function is perfect and the resource consumption problems.
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 using System. web. mvc; 6 using System. componentModel; 7 using System. componentModel. dataAnnotations; 8 9 namespace Base10 {11 /// <summary> 12 // all models of the basic model inherit from him 13 /// </summary> 14 public class BaseModel15 {16 [Key] 17 public long Idx {get; set;} 18 19 public DateTime EventDateTiem {get; set;} 20} 21 22 23 p Ublic class BaseController <T>: Controller24 where T: BaseModel, new () 25 {26 # region private variable 27 private class Entities: System. data. entity. dbContext28 {29 public System. data. entity. dbSet <T> TModel {get; set;} 30} 31 private Entities db = new Entities (); 32 private dynamic TModel = new T (); 33 # endregion34 35 public BaseController () 36 {37} 38 39 # add, delete, modify, and query 40 public virtual ActionResult View (int Idx = 0) 41 {42 var ViewModel = db. TModel. firstOrDefault (p => p. idx = Idx); 43 return View (ViewModel); 44} 45 46 public virtual ActionResult Create () 47 {48 return View (); 49} 50 51 [HttpPost] 52 public virtual ActionResult Create (T Model) 53 {54 55 if (ModelState. isValid) 56 {57 db. TModel. add (Model); 58 db. saveChanges (); 59 return Redirect ("/" + TModel. getType (). name + "/List? Message = added "); 60} 61 return View (Model); 62} 63 64 public virtual string Delete (int Idx = 0) 65 {66 return" Delete "; 67} 68 # endregion69 70} 71}
I have added only one
Use the Inheritance Method in the actual controller
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcApplication1.Controllers{ public class HomeController : Base.BaseController<Models.TestTable> { }}
Inherit the basic controller to the desired Home controller and set the model object required by this controller.
If you have special requirements to customize some operations, you can use
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcApplication1.Controllers{ public class HomeController : Base.BaseController<Models.TestTable> { public override ActionResult Create() { return base.Create(); } }}
In this way.
I use reflection to get the submitted model name
The data operation object is added to the Controller.
The operation can be customized through subsequent inheritance.
However, this seems to have broken the MVC specification.
Let's share our views ....
I wrote an example.
Http://files.cnblogs.com/mota/MvcApplication1.zip
You can download it and check it out.
Environment: VS 2012 RTM + WIN 7 64
MVC 3, EF 5.0
---------------------------
Correction
1. TModel. GetType () should be TModel. GetType (). Name
2. I did not write the resource release code in the basic controller. Sorry, I only provide a new idea and idea.