Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net foreach and sort projects

Tags:

c#

asp.net

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;
        }
    }
like image 733
foo bar Avatar asked Jan 19 '26 02:01

foo bar


1 Answers

By looking at your old view I think this is rather simple :

@using (Html.BeginForm("Index", "Projects", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    var allProjects = ViewData["allProjects"] as List<Project>;
    var allProjectStatuses = ViewData["allStatuses"] as List<ProjectStatus>;
    foreach (var project in allProjectStatuses)
    {
        <div>
            Html.RenderPartial("Projects", allProjects.Where(x => x.ProjectStatus.Name == project));
        </div>
    }
}

Update 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();

        // Set ViewData allStatuses to store all project statuses from DB
        ViewData["allStatuses"] = db.GetAllProjectStatuses().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();
}
like image 140
Syed Farjad Zia Zaidi Avatar answered Jan 21 '26 17:01

Syed Farjad Zia Zaidi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!