0

I am creating a Project Manager sort of app for self as a learning experience. I am using ASP.NET MVC3 and will be using jQuery to as well further down the line. For now I have created the models and some mock repositories using Ninject/Moq.

Model:

public class Project
{
    public int ProjectID { get; set; }

    public string ProjectName { get; set; }

    public string Description { get; set; }

    public string[] AssignedEmployees { get; set; }

    public string[] ProjectGoals { get; set; }

    public DateTime ProjectStart { get; set; }

    public DateTime ProjectDeadLine { get; set; }

    public string Notes { get; set; }
}

Mock repository:

Mock<IProjectRepository> projectMock = new Mock<IProjectRepository>();
        projectMock.Setup(m => m.Projects).Returns(new List<Project> 
        {
            new Project {ProjectID = 1234, ProjectName = "Email redesign", Description = "New email", AssignedEmployees = new string[] {"Ian", "Danny", "Mikey"}},
            new Project {ProjectID = 4321, ProjectName = "Update Cart", Description = "Make cart smoother function better"},
            new Project {ProjectID = 4567, ProjectName = "New social widget", Description = "More social media buttons"}
        }.AsQueryable());

        ninjectKernel.Bind<IProjectRepository>().ToConstant(projectMock.Object);

View:

@foreach  (var p in Model){
<div class='project'>
    <h3>@p.ProjectName</h3>
    <p>@p.Description</p>
    <ul class='assigned-employees'>
        <li class="employee">
        @p.AssignedEmployees
        </li>
    </ul>
</div>
}

The problem is that all that is rendered in the view for the AssignedEmployees array is System.String[]. I have tried .ToString, .ToArray and I have tried making a foreach loop to loop through each item in the array to print them each to a separate li.

How can I get it to print all the names in the array? Is using an array for this even the best solution? I would like so that anyone using the app can assign as many employees as they want to the project, that's why I thought an array might be best.

1 Answer 1

2

You should be able to use a foreach loop or a for loop. Calling .ToString() is just going to print System.String[] and calling .ToArray() on an array is not going to do anything

<ul class='assigned-employees'>
   @{foreach(var emp in p.AssignedEmployees){
       <li class="employee">@emp</li>
  }}
</ul>

or

<ul class='assigned-employees'>
   @{for(var i = 0; i<p.AssignedEmployees.Count(); i++){
       <li class="employee">@p.AssignedEmployees[i]</li>
  }}
</ul>

Also, I hope you are checking to see if the array exists before trying to enumerate it, else you will get a runtime exception

<ul class='assigned-employees'>
   @{if(p.AssignedEmployees != null && p.AssignedEmployes.Any()){
     foreach(var emp in p.AssignedEmployees){
       <li class="employee">@emp</li>
     }
  }}
</ul>
Sign up to request clarification or add additional context in comments.

3 Comments

That returns a "Object reference not set to an instance of an object." error on the foreach line.
Correct, please look at my last block of code. You are not always initializing the AssignedEmployees in your ViewModel. You will get a NullReferenceException in those cases. Thus, you need to check if it actually exists before you attempt to enumerate it.
Thanks. This is what I used that got it working: <ul class='assigned-employees'> @{if (p.AssignedEmployees != null) { foreach (var emp in p.AssignedEmployees) { <li class="employee">@emp</li> } }} </ul>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.