asp.net foreach and sort projects
NickName:foo bar Ask DateTime:2015-08-17T21:46:48

asp.net foreach and sort projects

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

Answers
Syed Farjad Zia Zaidi 2015-08-17T14:07:38

By looking at your old view I think this is rather simple :\n\n@using (Html.BeginForm(\"Index\", \"Projects\", FormMethod.Post, new { enctype = \"multipart/form-data\" }))\n{\n var allProjects = ViewData[\"allProjects\"] as List<Project>;\n var allProjectStatuses = ViewData[\"allStatuses\"] as List<ProjectStatus>;\n foreach (var project in allProjectStatuses)\n {\n <div>\n Html.RenderPartial(\"Projects\", allProjects.Where(x => x.ProjectStatus.Name == project));\n </div>\n }\n}\n\n\nUpdate Controller:\n\npublic ActionResult Index(FormCollection form)\n{\n using (DatabaseLayer db = new DatabaseLayer())\n {\n ViewData[\"projectList\"] = new SelectList(db.GetConsultantProjects(Constants.CurrentUser(User.Identity.Name)), \"ProjectId\", \"ProjectName\");\n ViewData[\"allProjects\"] = db.GetAllProjects().OrderByDescending(x => x.StartDate).ToList();\n\n // Set ViewData allStatuses to store all project statuses from DB\n ViewData[\"allStatuses\"] = db.GetAllProjectStatuses().ToList();\n if (form != null && form.AllKeys.Length > 0)\n {\n 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));\n db.UpdateConsultantProjectMemberships(Constants.CurrentUser(User.Identity.Name), projects);\n ViewData[\"posted\"] = true;\n }\n }\n return View();\n}\n",


More about “asp.net foreach and sort projects” related questions

asp.net foreach and sort projects

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 t...

Show Detail

foreach sort results from database

I have 9 blocks, and I want sort it by last news added. I have code: $projects = Projects::find(['conditions' =&gt; 'active = 1', 'order' =&gt; 'id DESC']); $itemsps = []; foreac

Show Detail

Sort ListBox in ASP.net

I have some code at my job that uses ASP.net (which I have never touched) but I need to sort it. Here is the ListBox that I need to sort by Dscrp: foreach (InteractiveInfo template in ddlsour...

Show Detail

Perl: foreach and sort

I'm writing a little program that I need to sort the results in. Basically, I have a foreach loop that gets its data from a subroutine. Here's the code: foreach $host (get_mirror_list()) { ...

Show Detail

ASP.Net MVC vs ASP.Net Forms

Why would you consider using ASP.Net MVC or standard ASP.Net with forms and controls for a web project? Apart from personal preference what would be the reasons? What sort of projects do you find m...

Show Detail

Looking for repeater type functionality in ASP.Net MVC

I am used to using a Repeater control in conventional ASP.Net web projects. I see ASP.Net MVC doesn't have this sort of thing. What should I be using here? EDIT: In response to the question, what...

Show Detail

Velocity foreach sort list

Can any one suggest how to sort this list in velocity ? So far I tried #foreach($it in ${pro.test}.sort(${pro.test.sortOrder})) ......... #end But it is throwing error, Also I tried...

Show Detail

Analyzing ASP.NET MVC Projects with Roslyn

I am currently working on a static source code analyzer for ASP.NET projects. My analyzer runs fine on ASP.NET WebForms but there seem to be issues with MVC projects. There are certain identifiers/...

Show Detail

Edit and continue in ASP.NET web projects

I know how to enable Edit and Continue in ASP.NET Web Application projects (see here), however, I found no information as to how to achieve the same thing in ASP.NET Web Site projects. Is this fea...

Show Detail

What is the naming convention for ASP.NET Core projects?

In ASP.Net Core the convention for projects seems to be to put the ASP.Net Core projects inside a src\ folder, and the test projects inside a test\ folder. What other conventions are there, ie. wh...

Show Detail