1

I am trying to display a list of objects in my asp.net view but it somehow isn't working.

VIEW

@model List<WCFasp.net.Models.ViewProject>
@{
    ViewBag.Title = "ShowSelectedView";
}

@ViewBag.sl

@for (int i = 0; i < Model.Count; i++)
{
    <p>@Html.DisplayFor(m => m[i].Name)</p>
}

Controller

public ActionResult Check(List<WCF.Project> selectedprojectlist)
{
    List<ViewProject> selectedlist = new List<ViewProject>();

    foreach (var dir in selectedprojectlist)
    {
        if (dir.IsChecked == true)
        {
            selectedlist.Add(new ViewProject(dir.Name, dir.Path, dir.Size, dir.LastChange, dir.IsChecked));
        }
    }

    ViewBag.sl = selectedlist.Count;

    return View("ShowSelectedView", selectedlist);
}

The ViewBag.sl displays Model.Count from the Controller. The Counter works and tells me there are objects in the list but it wont display them.

Does someone see an error or has an alternative idea?

4
  • Show me controller action code. you might have not return Model from action Commented Feb 4, 2016 at 10:37
  • give us a clean question. it is very confusing. show the controller and view code separately. Commented Feb 4, 2016 at 10:38
  • Check if <p> tag rendered or not? Commented Feb 4, 2016 at 10:42
  • @anand Do you see an error in the way I try to display it? Is it the correct way of displaying lists? Do you know an other way? Isn't that clear and you can just ask for the Controller. jeez Commented Feb 4, 2016 at 10:44

2 Answers 2

3

You can do like this

    foreach(var item in Model)
    {
       <p>@item.Name</p>
    }

Hope this helps

Sign up to request clarification or add additional context in comments.

9 Comments

Model.Count works fine also without brackets. It now displays "item.Name" and not the actual object Name. So I thought of adding an @ in front of item but then it displays nothing again.
Works now :) Had some trouble with the list that I am receiving as parameter.
What?? OP's model is List<T> which has a property Count so Model.Count does work God only knows who up voted this completely wrong answer. @Mr.Jackson, Your should not accept this - its misleading to other users that you have signaled this wrong answer works - it has nothing to do with your issue.
@Stephen Muecke is right. Sorry about that and i have updated the code. Thanks
@anand - Which now does exactly the same thing as OP's original code was doing (and adding a if(Model.Count > 0) block is just pointless). You answer has absolutely nothing to do with OP's problem.
|
0

You could use a foreach.

    @foreach(var item in Model)
    {
        <div>@item.Name</div>
    }

Comments

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.