首页 新闻 赞助 找找看

万恶的MVC+EF+easyui整死我了,一会增删改都成功,一会都失败,闹哪样?

0
悬赏园豆:10 [已解决问题] 解决于 2014-07-22 09:48


 

MVC4+EF实体框架+easyui做的小例子,崩溃边缘

我自己创建的实体类(EF自动创建的实体类用不了)Model:

 1 namespace BlogModels
 2 {
 3     public class LogInfo
 4     {
 5         public int LogID { get; set; }
 6         public int LogTypeID { get; set; }
 7         public string LogTitle { get; set; }
 8         public string LogContent { get; set; }
 9         public string LogDate { get; set; }
10         public string LogTypeName { get; set; }
11     }
12 }
View Code

DAL:是EF框架


BLL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BlogDAL;
using BlogModels;

namespace BlogBLL
{
    public class LogBLL
    {
        static BlogEntities db = new BlogEntities();
        public static List<LogInfo> GetLogList()
        {
            var obj = db.Log.ToList();
            var list = new List<LogInfo>();
            foreach (var item in obj)
            {
                list.Add(new LogInfo { LogID=item.LogID,LogTypeName=item.LogType.LogTypeName,LogTitle=item.LogTitle,LogContent=item.LogContent,LogDate=item.LogDate.ToString()});
            }
            return list;
        }

        #region AddLog
        public static bool AddLog(LogInfo obj)
        {
            var isok = false;
            Log l = new Log();
            l.LogID = 0;
            l.LogTypeID = obj.LogTypeID;
            l.LogTitle = obj.LogTitle;
            l.LogContent = obj.LogContent;
            l.LogDate =Convert.ToDateTime(obj.LogDate);
            db.Log.Add(l);
            var i = db.SaveChanges();
            if (i > 0)
            {
                isok = true;
            }
            return isok;
        } 
        #endregion

        public static Log GetLogByID(int id)
        {
            return db.Log.SingleOrDefault(a => a.LogID == id);
        }

        public static bool EditLog(int id,LogInfo lg) {
            bool isok = false;
            var obj = db.Log.SingleOrDefault(a=>a.LogID==id);
            if(obj!=null){
                obj.LogTypeID = lg.LogTypeID;
                obj.LogTitle = lg.LogTitle;
                obj.LogContent = lg.LogContent;
                obj.LogDate =Convert.ToDateTime(lg.LogDate);
                if(db.SaveChanges()>0){
                    isok = true;
                }
            }
            return isok;
        }

        public static bool DelLog(int id) {
            var isok = false;
            var obj = db.Log.SingleOrDefault(a=>a.LogID==id);
            if(obj!=null){
                db.Log.Remove(obj);
                var i = db.SaveChanges();
                if(i>0){
                    isok = true;
                }
            }
            return isok;
        }
        
    }
}
View Code

UI(MVC4控制器代码):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlogBLL;
using BlogModels;

namespace BlogUI.Controllers
{
    public class LogController : Controller
    {
        //
        // GET: /Log/

        public ActionResult Index()
        {

            return View();
        }
                    
        public ActionResult GetList()
        {
            var obj = LogBLL.GetLogList();
            return Json(obj);
        }
        public ActionResult Add(LogInfo obj) {
            var id = obj.LogID;
            if (id!=0)
            {
                var result = LogBLL.EditLog(id, obj);
            }
            else
            {
                var result = LogBLL.AddLog(obj);
            }
            return View();
        }

        public ActionResult Edit(int id,LogInfo lg) {
            var result = LogBLL.EditLog(id,lg);
            return View();
        }

        public JsonResult Del(int id) {
            var result = LogBLL.DelLog(id);
            return Json("true");
        }

    }
}
View Code

UI(视图代码):

@{
    ViewBag.Title = "Index";
}


<label>@Session["name"]欢迎您!</label>

<table id="tab" class="easyui-datagrid" data-options="url:'@Url.Action("GetList", "Log")',toolbar:'#toolbar'">
    <thead>
        <tr>
            <th data-options="field:'LogID',title:'编号'"></th>
            <th data-options="field:'LogTypeName',title:'类型'"></th>
            <th data-options="field:'LogTitle',title:'标题'"></th>
            <th data-options="field:'LogContent',title:'内容'"></th>
            <th data-options="field:'LogDate',title:'创建时间'"></th>
        </tr>
    </thead>
</table>

<div id="dlg" class="easyui-dialog" style="width: 400px; height: 200px; padding: 10px 20px" closed="true" buttons="#dlg-buttons">
    <div class="ftitle"></div>
    <form id="fm" method="post" novalidate>
        @*<div class="fitem">
            <label>编号:</label>
            <input name="LogID" class="easyui-validatebox" required="true" />
        </div>*@
        <input type="hidden" name="LogID" />
        <div class="fitem">
            <label>类型ID:</label>
            <input name="LogTypeID" class="easyui-validatebox" required="true" />
        </div>
        <div class="fitem">
            <label>标题:</label>
            <input name="LogTitle" />
        </div>
        <div class="fitem">
            <label>内容:</label>
            <input name="LogContent" />
        </div>
        <div class="fitem">
            <label>时间:</label>
            <input name="LogDate" />
        </div>
    </form>
</div>

<div id="toolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newUser()">添加</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editUser()">修改</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="destroyUser()">删除</a>
</div>

<div id="dlg-buttons">
    <a id="add" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveUser()">Save</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Cancel</a>
</div>

<script type="text/javascript">
    var url;
    function newUser() {
        $("#dlg").dialog("open").dialog("setTitle", "添加");
        $("#fm").form("clear");
        url = '@Url.Action("Add", "Log")';
    }


    function saveUser() {
        $("#fm").form('submit', {
            url: '@Url.Action("Add", "Log")',
            onSubmit: function (param) {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');
                }
                return isValid;
            },
            success: function () {
                $.messager.progress('close');
                $("#tab").datagrid("reload");
            }
        });

    }

    function editUser() {
        var row = $("#tab").datagrid("getSelected");
        if (row) {
            $("#dlg").dialog("open").dialog("setTitle", "修改");
            $("#fm").form("load", row);
            $("add").click(function () {
                $.post({
                    url: '@Url.Action("Edit", "Log")',
                    data: {
                        id: row.LogID
                    },
                    success: function (result) {
                        if (result) {
                            $("#tab").datagrid("reload");
                        } else {
                            $.messager.show({
                                title: "Error",
                                msg: result.errorMsg
                            });
                        }
                    },
                    dataType: "json"
                })
            });
        }

    }


    function destroyUser() {
        var row = $("#tab").datagrid("getSelected");
        if (row) {
            $.messager.confirm("Confirm", "是否删除?", function (r) {
                if (r) {
                    $.post('@Url.Action("Del", "Log")', { id: row.LogID }, function (result) {
                        if (result == "true") {
                            $("#tab").datagrid("reload");
                        } else {
                            $.messager.show({
                                title: "Error",
                                msg: result.errorMsg
                            });
                        }
                    }, "json");
                }
            });
        }
        url = '@Url.Action("Del", "Log")';
    };
</script>
View Code

BUG太多!!

 

骑着蜗牛耍流氓的主页 骑着蜗牛耍流氓 | 初学一级 | 园豆:135
提问于:2014-07-21 16:38
< >
分享
最佳答案
0

慎用,过度的封闭带来的是操作的方便,但灵活有的时候会受影响。五表以上的关联查询以及复杂业务系统,还是考虑用ADO.NET。局部的单表增删改查可以用EF,MVC的架构的觉得没多问题,用熟了就好。

收获园豆:10
jerry-Tom | 老鸟四级 |园豆:4077 | 2014-07-21 16:53

哦,什么药慎用啊?本人菜鸟

骑着蜗牛耍流氓 | 园豆:135 (初学一级) | 2014-07-21 16:55

@骑着蜗牛耍流氓: EF的适用场合是考虑好,根据系统的负责度考虑是否用EF。

jerry-Tom | 园豆:4077 (老鸟四级) | 2014-07-21 16:56

@jerry-Tom: 公司项目是用EF的  所以我再学习EF

骑着蜗牛耍流氓 | 园豆:135 (初学一级) | 2014-07-21 17:23

@骑着蜗牛耍流氓: 哦,这样了,了然。

jerry-Tom | 园豆:4077 (老鸟四级) | 2014-07-21 17:30
其他回答(1)
0

使用moon.orm

去我博客看看

迅捷网络[来送福利] | 园豆:616 (小虾三级) | 2014-07-21 21:17

好的

支持(0) 反对(0) 骑着蜗牛耍流氓 | 园豆:135 (初学一级) | 2014-07-22 09:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册