本文介绍了我如何在dbcontext.set中使用where条件< tentity>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MVC中使用Edit进行多项选择。所以我做了一个Jsonresult来获取该ID的数据: -



我尝试过的事情:



I want to use Edit in MVC for multiple selection . So I've made a Jsonresult to fetch the Data for that Id :-

What I have tried:

<pre>public JsonResult FetchDataForEdit()
        {
            int IDtoEdit = Convert.ToInt32(TempData["IDtoEdit"]);
            string MyTableName = Convert.ToString(TempData["MyTableName"]);
            
            try
            {
                Type tableType = typeof(CourseDesc);
                switch (MyTableName)
                {
                    case "CourseTbl":
                        tableType = typeof(CourseTbl);
                        break;
                    case "CourseDescTbl":
                        tableType = typeof(CourseDesc);
                        break;
                    case "CourseSubDesc":
                        tableType = typeof(CourseSubDesc);
                        break;
                    case "InternTbl":
                        tableType = typeof(InternShip);
                        break;
                    case "ContactTbl":
                        tableType = typeof(Contact);
                        break;
                }

                using (EBContext db = new EBContext())
                {
                    var results = new List<object>();
                    foreach (var item in db.Set(tableType))
                    {
                        //Want to Add result for selected IDtoEdit here
                        results.Add(item);
                    }

                    return new JsonResult { Data = results, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
                }

            }

            catch (Exception ex)
            {
                string innerMessage = (ex.InnerException != null) ? ex.InnerException.Message : "";
                return new JsonResult { Data = "Not Found", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            }

        }

推荐答案

using (var db = new EBContext())
{
    var results = new List<object>();
    var item = db.Set(tableType).Find(IDtoEdit);
    if (item != null) results.Add(item);
    
    return Json(results, JsonRequestBehavior.AllowGet);
}



[]


这篇关于我如何在dbcontext.set中使用where条件&lt; tentity&gt;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:14