显示记录外键的标题

时间:2018-03-06 08:48:46

标签: c# asp.net asp.net-mvc

美好的一天!遇到问题,有一个表,4个字段(Id,Parent Id,Title,Created,Description),其中Parent Id是同一个表中Id的外键。 客户端如何在表中显示不是ParentId,即父记录Title,如果有的话?

类别:

public class MyClass
    {
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string Title { get; set; }
        public DateTime Created { get; set; }
        public string Description { get; set; }
    }

控制器:

        public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {

            Repository repository = new Repository();

            ViewBag.CurrentSort = sortOrder;

            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";

            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;

            var records = from s in repository.GetMyClass()  select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                records = records.Where(s => s.Title.ToUpper().Contains(searchString.ToUpper()));
            }



            switch (sortOrder)
            {
                case "Name desc":
                    records = records.OrderByDescending(s => s.Title);
                    break;
                case "Date":
                    records = records.OrderBy(s => s.Created);
                    break;
                case "Date desc":
                    records = records.OrderByDescending(s => s.Created);
                    break;
                default:
                    records = records.OrderBy(s => s.Title);
                    break;
            }

            int pageSize = 4;
            int pageIndex = (page ?? 1);



            return View("_TableRecords",records.ToPagedList(pageIndex, pageSize));
        }

2 个答案:

答案 0 :(得分:1)

您需要实施自我加入。

首先,您需要向ViewModel添加一个字段:

IEnumerable<MyClass> myData = 
    from m in t.YourEntity
    join e1 in t.YourEntity on m.ParentId equals e1.Id
    select new MyClass
    { 
        Id = m.Id,
        ParentId = m.ParentId,
        Title = m.Title,
        ParentTitle = e1.Title,
        Created = m.Created,
        Description = m.Description
    };

    return View(myData.ToList());

然后改变获取数据的方式,应该是这样的:

{{1}}

答案 1 :(得分:1)

试试这个,在sql脚本中选择所有数据,包括父标题,如下所示:

DECLARE @DemoTable TABLE(
Id INT,
ParentId INT,
Title NVARCHAR(50),
Created DATETIME,
Description NVARCHAR(50))

INSERT INTO @DemoTable VALUES(1, NULL, 'Fruit', '2018-03-06 17:00:00.000', '')
INSERT INTO @DemoTable VALUES(2, 1, 'Apple', '2018-03-06 17:00:00.000', '')
INSERT INTO @DemoTable VALUES(3, 1, 'Banana', '2018-03-06 17:00:00.000', '')
INSERT INTO @DemoTable VALUES(4, NULL, 'Sport', '2018-03-06 17:00:00.000', '')
INSERT INTO @DemoTable VALUES(5, 4, 'Baseball', '2018-03-06 17:00:00.000', '')
INSERT INTO @DemoTable VALUES(6, 4, 'Football', '2018-03-06 17:00:00.000', '')


SELECT T1.Id, T1.Title, T1.ParentId, T2.Title AS 'ParentTitle'
FROM @DemoTable AS T1
LEFT JOIN @DemoTable AS T2
ON T1.ParentId = T2.Id

执行结果
enter image description here

您可以获取所有数据,包括父标题
然后,将您的viewmodel更改为:

public class MyClass
    {
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string ParentTitle { get; set; }
        public string Title { get; set; }
        public DateTime Created { get; set; }
        public string Description { get; set; }
    }