在我只有一个视图模型之前,但现在我已经创建了一个包含两个视图的模型:
public class ApplicationTwoViewModel
{
public IEnumerable<Project> Model1 { get; set; }
public Application Model2 { get; set; }
}
之前的分页工作,但现在我必须处理Model1和Model2,它不再有效,并且很难修复它。 这是我到目前为止所尝试的:
public class ProjectsController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Projects
[AllowAnonymous]
public ActionResult Index(string SortOrder, string SortBy, string Page)
{
ViewBag.SortOrder = SortOrder;
ViewBag.SortBy = SortBy;
var projects = db.Projects.ToList();
var model = new ApplicationTwoViewModel
{
Model1 = new List<Project>(projects),
Model2 = new Application
{
//hard coded, needs change
UserId = User.Identity.GetUserId(),
ProjectId = 11,
CoverLetter = "asdf",
ApplicationId = 23,
}
};
switch (SortBy)
{
case "Title":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.Title).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.Title).ToList();
break;
default:
break;
}
break;
case "ApplicationDeadline":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.ApplicationDeadline).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.ApplicationDeadline).ToList();
break;
default:
break;
}
break;
case "Duration":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.Duration).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.Duration).ToList();
break;
default:
break;
}
break;
case "HourlyRate":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.HourlyRate).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.HourlyRate).ToList();
break;
default:
break;
}
break;
case "TotalProjectCost":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.TotalProjectCost).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.TotalProjectCost).ToList();
break;
default:
break;
}
break;
case "City":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.City).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.City).ToList();
break;
default:
break;
}
break;
case "RequiredPresencePercent":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.RequiredPresencePercent).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.RequiredPresencePercent).ToList();
break;
default:
break;
}
break;
case "Language":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.Language).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.Language).ToList();
break;
default:
break;
}
break;
case "RequiredSkills":
switch (SortOrder)
{
case "Asc":
projects = projects.OrderBy(x => x.RequiredSkills).ToList();
break;
case "Desc":
projects = projects.OrderByDescending(x => x.RequiredSkills).ToList();
break;
default:
break;
}
break;
default:
projects = projects.OrderBy(x => x.Title).ToList();
break;
}
ViewBag.TotalPages = Math.Ceiling(db.Projects.ToList().Count()/10.0);
int page = int.Parse(Page == null ? "1" : Page);
ViewBag.Page = page;
// before it was projects = projects.Skip ....
model.Model1 = model.Model1.Skip((page - 1) * 10).Take(10).ToList();
return View(model);
}
答案 0 :(得分:1)
您正在对项目进行排序,但未设置为传递给View的模型添加此行:
// before it was projects = projects.Skip ....
model.Model1 = projects; // <-- this line
model.Model1 = model.Model1.Skip((page - 1) * 10).Take(10).ToList();
return View(model);