Asp.net mvc 无法从控制器检索数据

Asp.net mvc 无法从控制器检索数据,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc 3,我的控制器代码就像我返回数据一样 public ActionResult StudentDashboard(int? page) { ViewBag.StudentRequest = GetData.Tables[0].AsEnumerable().Select(t => new { StudentRequestId = t.Field&l

我的控制器代码就像我返回数据一样

public ActionResult StudentDashboard(int? page)
{    
    ViewBag.StudentRequest = GetData.Tables[0].AsEnumerable().Select(t => new
                                {
                                    StudentRequestId = t.Field<int>("StudentRequestId"),
                                    ClassName = t.Field<string>("ClassName"),
                                    CreatedOn = t.Field<DateTime>("CreatedOn"),
                                    Location = t.Field<string>("Location"),
                                    PaymentMethod = t.Field<string>("PaymentMethod"),
                                }).ToList().ToPagedList(page ?? 1,1);

    return View(db.StudentRequests.Where(x => x.RegistrationId ==  registrationid).ToList().ToPagedList(page ?? 1, 3));
}
在视图方面,我编写了如下代码

@using PagedList;
@using PagedList.Mvc;
@model IPagedList<Student_Tutor.Models.StudentRequest>

   @if (ViewBag.StudentRequest != null)
    { 
      var StudentRequestId = (int)Model.First().StudentRequestId;// Here I am able to get the StudentRequestId 
      var StudentRequestTimecount = StudentRequestTime.Where(d => d.StudentRequestId == StudentRequestId).ToList();
      var TutorStudentRequestcount = TutorStudentRequest.Where(d => d.StudentRequestId == StudentRequestId).ToList();
      @Html.LabelFor(model => model.First().StudentRequestId)// here only text is displaying as StudentRequestId
      @Html.DisplayNameFor(Model => Model.First().CreatedOn)//here only text is diplaying as created on
}
通过这段代码,我无法获得StudentRequestId的Id号,而不是视图侧仅显示文本的编号

请参见此屏幕截图:

LabelFor helper方法使用属性名称或DisplayName属性(如果用于装饰属性)呈现标签元素

如果要打印属性值,可以使用DisplayFor helper方法

@Html.DisplayFor(model => model.First().StudentRequestId)

我也尝试过使用display for,但它仍然显示与我尝试在上创建的文本相同的文本,但它仅在StudentRequestId具有有效值时显示文本,DisplayFor帮助程序将呈现该结果。我刚刚在视图中使用DisplayFor尝试了您的代码,它确实打印了该代码的值。在我的问题中,我在这一行上还写了一些注释var StudentRequestId=intModel.First.StudentRequestId;//在这里,我可以得到StudentRequestId…..我可以得到这个值,那么它没有在DisplayFor中显示该值可能有什么问题
@Html.LabelFor(model => model.First().StudentRequestId)
@Html.DisplayFor(model => model.First().StudentRequestId)