Easyui-DataGrid 的增删查改

参考:jQuery & jEasyUI Extensions

参考:基于MVC4+EasyUI的Web开发框架形成之旅--界面控件的使用

Easyui-DataGrid 的增删查改_第1张图片

  Easyui-DataGrid 的增删查改_第2张图片

Demo视图

@{
    Layout = null;
}





    
    DataGrid的使用示例
    
    
    
    
    
    


    


Home控制器

using MVC.Easyui.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace MVC.Easyui.Controllers
{
    public class HomeController : Controller
    {
        //进入视图-->HTML的写法
        public ActionResult Index()
        {
            return View();
        }

        //进入视图-->JS的写法
        public ActionResult Demo()
        {
            return View();
        }

        //视图页面的Easyui-DataGrid通过异步请求数据
        public string GetData(FromData fd)
        {
            salesEntities1 db = new salesEntities1();

            int count = db.T_UserInfo.Count();

            //根据指定的字段来进行排序
            //var list = db.T_UserInfo.AsEnumerable().OrderBy(p => GetPropertyValue(p, fd.sort)).Skip((fd.page - 1) * (fd.rows)).Take(fd.rows).ToList();

            var list = db.T_UserInfo.AsEnumerable();

            //如果用户设置了排序(即:传递了sort和order参数过来)sort表示要排序的字段,order表示升序还是降序排序
            if (!string.IsNullOrEmpty(fd.sort) && !string.IsNullOrEmpty(fd.order))
            {
                //根据指定字段来排序并分页
                if (fd.order == "DESC")  
                {
                    list = list.OrderByDescending(r => r.GetType().GetProperty(fd.sort).GetValue(r)).Skip((fd.page - 1) * fd.rows).Take(fd.rows).AsQueryable();
                }
                else
                {
                    list = list.OrderBy(r => r.GetType().GetProperty(fd.sort).GetValue(r)).Skip((fd.page - 1) * fd.rows).Take(fd.rows).AsQueryable();
                }
            }
            else
            {
                list = list.OrderBy(r => r.Id).Skip((fd.page - 1) * fd.rows).Take(fd.rows).AsQueryable();
            }



            //如果搜索框的内容不为空,就进行搜索查询
            if (!string.IsNullOrEmpty(fd.searchName))
            {
                list = list.Where(r => r.Name.Contains(fd.searchName));//模糊查询
            }
            var x = new
            {
                total = count,
                rows = list.Select(r => new   //在这里来构建匿名类对象
                {
                    Id = r.Id,
                    Name = r.Name,
                    Age = r.Age,
                    Phone = r.Mobile,
                    Email = r.Email
                })
            };
            //使用Jil.JSON.Serialize()来对对象序列化需要通过安装插件 PM> Install-Package Jil -version 2.10.0
            var strJson = Jil.JSON.Serialize(x);
            return strJson;
        }

        //批量删除
        public int RemoveDataById(string id)
        {
            salesEntities1 db = new salesEntities1();
            string[] ids = id.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            

            List list = new List();

            int userid = 0;
            for (int i = 0; i < ids.Length; i++)
            {
                userid = int.Parse(ids[i]);
                var user = db.Set().FirstOrDefault(r => r.Id == userid);
                list.Add(user);
            }
            list.ForEach(r => db.Set().Remove(r));
            return db.SaveChanges();
        }

        //新增或编辑数据(根据Id是否为空值来判断是新增还是修改)
        public int AddOrEditData(FromData row)
        {
            salesEntities1 db = new salesEntities1();
            //var rowData = Request.Form["row"];
            if (row != null)
            {
                
                //新增
                if (row.Id == 0)
                {
                    T_UserInfo user = new T_UserInfo();
                    user.Name = row.Name;
                    user.Age = row.Age;
                    user.Mobile = row.Phone;
                    user.Email = row.Email;
                    user.Gender = 1;
                    user.LoginId = 1;
                    db.T_UserInfo.Add(user);
                }
                else
                {
                    T_UserInfo model = new T_UserInfo();
                    model.Id = row.Id;
                    model.Name = row.Name;
                    model.Age = row.Age;
                    model.Mobile = row.Phone;
                    model.Email = row.Email;

                    model.Gender = 1;
                    model.LoginId = 1;
                    System.Data.Entity.Infrastructure.DbEntityEntry entity = db.Entry(model);
                    entity.State = System.Data.EntityState.Modified;

                }
                return db.SaveChanges();
            }
            return 0;

          
        }


        /*
        /// 
        /// 根据传递进来的对象+对象的某个属性来返回指定对象的该属性值
        /// 
        /// 传入类型对象
        /// 对象的某个属性
        /// 返回指定对象的该属性值
        private object GetPropertyValue(object obj, string property)
        {
            System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
            return propertyInfo.GetValue(obj, null);
        }*/
    }



    //用于接收从视图传递过来的表单参数
    public class FromData
    {
        //-----------------------以下用于分页

        //当前页
        public int page { get; set; }
        //页大小
        public int rows { get; set; }

        //-----------------------以下用于搜索

        //搜索的姓名
        public string searchName { get; set; }

        //----------------------以下用于排序

        //按哪个字段进行排序
        public string sort { get; set; }

        //排序方式(DESC ,ASC)
        public string order { get; set; }


        //-----------以下是用于新增或编辑数据
        public int Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }

        public string Phone { get; set; }

        public string Email { get; set; }
    }

    
}





你可能感兴趣的:(EasyUI)