在一个控制器中返回两组动作

时间:2013-05-05 23:07:17

标签: c# asp.net-mvc

所以我正在做的是我在页面中显示类别列表,每个类别包含一个类别子列表。我有一个控制器,它返回没有类别子列表的类别列表。如何使用相同的控制器显示子列表。

控制器:

 public CourseIndexVw Get(int id)
    {
        var _types = new ElementTypesService().GetElementModelsForCourseIndex(id, WebSecurity.CurrentUserId);
        var _courseIndexbyTypesVw = new CourseSectionsControllerHelper().CourseIndexTypeVw(id);
        _courseIndexbyTypesVw.Types = _types.ToList();
        var _activeType = _courseIndexbyTypesVw.Types.First();
        _courseIndexbyTypesVw.ActiveId = _activeType != null ? _activeType.Id : -1;
        return _courseIndexbyTypesVw;
    }

GetElementModelsForCourseIndex:

public List<ElementModelForCourseIndex> GetElementModelsForCourseIndex(int elementId, int userId, int depthLevel = 2)
        {
            List<ElementModelForCourseIndex> TypesName;
            ElementType type;
            using (var db = DataContextManager.AleStoredProcsContext)
            {

                TypesName = db.GetElementModelsForCourseIndex<ElementModelForCourseIndex>(elementId, userId, r => new ElementModelForCourseIndex{
                        Id = ElementsModelsForCourseIndexMap.Id(r),
                        Identity = ElementsModelsForCourseIndexMap.Identity(r)
                    }).OrderBy(n=>n.Identity).ToList();
            }
            foreach (ElementModelForCourseIndex typeContent in TypesName)
            {
                typeContent.Children = GetElementChildrenModelsForCourseIndex(elementId, userId, type.ModelId, depthLevel);
            }
        }

GetElementChildrenModelsForCourseIndex:

    public List<ElementModelForCourseIndex> GetElementChildrenModelsForCourseIndex(int elementId, int userId, ElementType typeId, int depthLevel = 2)
    {
        using (var db = DataContextManager.AleStoredProcsContext)
        {
            return db.GetElementWithCalendarAndPermsByModel<ElementModelForCourseIndex>(elementId, userId, typeId.Id, r => new ElementModelForCourseIndex
            {
                IdentityName = ElementsModelsForCourseIndexMap.IdentityName(r),
                ValueString = ElementsModelsForCourseIndexMap.ValueString(r),
                TimeReleased = ElementsModelsForCourseIndexMap.TimeReleased(r),
                TimeDue = ElementsModelsForCourseIndexMap.TimeReleased(r)
            }).OrderBy(i => i.IdentityName).ToList();
        }
    }

更新 当前问题是typeContent.Children = GetElementChildrenModelsForCourseIndex(elementId, userId, type.ModelId, depthLevel);我得到的错误是:override方法有无效的参数 任何帮助表示赞赏,如果我遗漏任何信息,请告知我们。谢谢!

1 个答案:

答案 0 :(得分:1)

您可以修改模型并添加children属性:

public class ElementModelForCourseIndex
{
    // *snip* your code
    public List<ElementModelForCourseIndex> Children {get; set;}
}

您可以在当前的GetElementModelsForCourseIndex中获取它,也可以使用这样的辅助方法:

public List<ElementModelForCourseIndex> GetElementModelsForCourseIndex(int elementId, int userId, int depthLevel = 2)
    {
        List<ElementModelForCourseIndex> courses;
        using (var db = DataContextManager.AleStoredProcsContext)
        {
            courses =  db.GetElementModelsForCourseIndex<ElementModelForCourseIndex>(elementId, userId, r => new ElementModelForCourseIndex{
                    Id = ElementsModelsForCourseIndexMap.Id(r),
                    Identity = ElementsModelsForCourseIndexMap.Identity(r)
                }).OrderBy(n=>n.Identity).ToList();
        }
        for each(ElementModelForCourseIndex course in courses)
        {
            // here you are filling the Children. 
            //You need to check if the parameters are the correct ones. 
            // Since you haven't shown the actual model class, I'm only guessing the parameters
            course.Children = GetElementChildrenModelsForCourseIndex(elementId, userId, depthLevel);
        }
        return courses;
    }