如何使用cshtml中的对象列表绑定数据

时间:2017-05-23 07:09:39

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

public ActionResult CCEEvaluationGetStudentMarks(int BranchID, int ClassID, int BranchSectionID)
{
    List<object> subjectData = BindSubjects(BranchID, ClassID, BranchSectionID);

    ViewBag.subjectData = subjectData.ToList();
}

public List<object> BindSubjects(int BranchID, int ClassID, int BranchSectionID)
{
    // return db.GetAcademicSubjectDetailsforDEO(ClassID, dts.AcademicYearID).ToList();

    try
    {
        dts = GetLogOnSessionModel(); dts.PageName = "/" + this.GetType().Name + "/" + System.Reflection.MethodBase.GetCurrentMethod().Name;

        List<object> d = new List<object>();

        if (dts.AccessLevelID == 5)
        {
            // staff
            if (dts.IsClassTeacher)
            {
                // classteacher
                List<GetAcademicStaffSubjectDetailsByClassTeacher_Result> data = db.GetAcademicStaffSubjectDetailsByClassTeacher(dts.PersonalID, BranchID, dts.AcademicYearID, ClassID).ToList();
                d = data.Cast<object>().ToList();
            }
            else
            {
                // ds = Sub.GetAcademicStaffSubjectDetails(int.Parse(ddlClass.SelectedValue), CoursesSession.Courses.AcademicYearID, StaffSession.Staff.StaffId, 1);
            }
        }
        else
        {
            List<GetAcademicSubjectDetailsforDEO_Result> data = db.GetAcademicSubjectDetailsforDEO(ClassID, dts.AcademicYearID).ToList();
            d = data.Cast<object>().ToList();
        }

        return d;
    }
    catch (Exception)
    {
        throw;
    }
}

在C#中我使用不同的存储过程,所以我把它放在一个对象列表中并发送到视图,这样你怎么能绑定list<object>

@foreach (var subjectHeader in ViewBag.subjectData as List<object>)
{
    <th>
        @subjectHeader
    </th>
}

我是razor的新手请帮助我如何绑定viewbag而不使用只使用对象列表的类文件,如果有可能的话......?

0 个答案:

没有答案