doing some asp.net mvc5 to get more familiar with it.
I've made it so I have projects, and each project have a status etc working in progress, closed, finished etc..
What I Want to do is I want to make a foreach loop to Make a list of the project statuses and than list the project with the right status under that tab.
This is what i've done so far
@using (Html.BeginForm("Index", "Projects", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
var allProjects = ViewData["allProjects"] as List<Project>;
foreach (var project in allProjects)
{
<div>project.ProjectStatus.Name</div>
}
}
How ever this would just list the project status of each project in the projectlist, so I get 1, work in progress than 10 finished.
This is my old view
if (allProjects != null)
{
<div class="panel-group accordion" id="accordion1">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#collapse_1">
Inhouse projekt
</a>
</h4>
</div>
<div id="collapse_1" class="panel-collapse collapse">
<div class="panel-body">
@{
Html.RenderPartial("Projects", allProjects.Where(x => x.ProjectStatu.Name == "Pågående - Inhouse"));
}
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#collapse_2">
Outhouse projekt
</a>
</h4>
</div>
<div id="collapse_2" class="panel-collapse collapse">
<div class="panel-body">
@{
Html.RenderPartial("Projects", allProjects.Where(x => x.ProjectStatu.Name == "Pågående - Outhouse"));
}
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#collapse_4">
Avslutade projekt
</a>
</h4>
</div>
<div id="collapse_4" class="panel-collapse collapse">
<div class="panel-body">
@{
Html.RenderPartial("Projects", allProjects.Where(x => x.ProjectStatu.Name == "Avslutat"));
}
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#collapse_3">
Övriga projekt
</a>
</h4>
</div>
<div id="collapse_3" class="panel-collapse collapse">
<div class="panel-body">
@{
Html.RenderPartial("Projects", allProjects.Where(x => x.ProjectStatu.Name != "Pågående - Inhouse" && x.ProjectStatu.Name != "Pågående - Outhouse" && x.ProjectStatu.Name != "Avslutat"));
}
</div>
</div>
</div>
</div>
Like we can see here I did just hard code the project status, and than render each project under that status if it matches the string.
But i'd rather not hard code this incase I were to add another project status at a later date I would have to change the view as well.
And my project Controller.
[Authorize]
public class ProjectsController : Controller
{
public ActionResult Index(FormCollection form)
{
using (DatabaseLayer db = new DatabaseLayer())
{
ViewData["projectList"] = new SelectList(db.GetConsultantProjects(Constants.CurrentUser(User.Identity.Name)), "ProjectId", "ProjectName");
ViewData["allProjects"] = db.GetAllProjects().OrderByDescending(x => x.StartDate).ToList();
if (form != null && form.AllKeys.Length > 0)
{
var projects = from x in form.AllKeys where x.Length >= 3 && x.Substring(0, 3) == "cb-" && form[x] == "true,false" select new Guid(x.Substring(3));
db.UpdateConsultantProjectMemberships(Constants.CurrentUser(User.Identity.Name), projects);
ViewData["posted"] = true;
}
}
return View();
}
public ActionResult Projects()
{
return View();
}
public ActionResult ConsulantTimes()
{
using (DatabaseLayer db = new DatabaseLayer())
{
ViewData["times"] = db.GetConsultantProjectTimes(Constants.CurrentUser(User.Identity.Name)).OrderByDescending(x => x.StartDate).ToList();
}
return PartialView();
}
public ActionResult Delete(Guid? id)
{
if (id.HasValue)
new DatabaseLayer().DeleteConsultantProjectTime(id.Value);
return null;
}
public ActionResult Update(Guid? projectId, DateTime? startDate, DateTime? endDate, int? pct, Guid? id)
{
if (projectId.HasValue && startDate.HasValue && endDate.HasValue && pct.HasValue)
new DatabaseLayer().UpdateConsultatProjectTime(projectId.Value, Constants.CurrentUser(User.Identity.Name), startDate.Value, endDate.Value, pct.Value, id);
return null;
}
}
Copyright Notice:Content Author:「foo bar」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/32052089/asp-net-foreach-and-sort-projects