Search code examples
asp.net-corelistview.net-coreasp.net-core-mvc

The list does not come up (ASP.NET Core MVC)


Thanks for helping me. Sorry for long description... I try to be detailed

This is my ProcessController:

namespace MVCDemo.Controllers
{
    public class ProcessController : Controller
    {
        // GET Process
        public ViewResult List()
        {
            var processList = from p in Process.GetProcesses()
                              select p;
            ViewData.Model = processList.ToList();
            return View();
        }

        // GET Process/Details/5
        public ActionResult Details(int id)
        {
            var process = (from p in Process.GetProcesses()
                           where p.Id == id
                           select p).Single();
            return View(process);
        }
    }
}

This is my Details.cshtml view:

@this.Model.ProcessName <br />
@this.Model.Id

This is my List.cshtml view:

<div>
    <h1>List</h1>
    <ul>
        @foreach (var process in this.Model)
        {
            <li> 
                @Html.ActionLink("process.ProcessName", "Details", new { id = process.Id })
            </li>
        }
    </ul> 
</div>

My problem is that every line I only can see "process.ProcessName" instead of a list what I would see if my List.cshtml would look this way:

<div>
    <h1>List</h1>
    <ul>
        @foreach (var process in this.Model)
        {
            <li>
                @process.ProcessName
            </li>
        }
    </ul>
</div>

I can see the problem is probably with this line

 @Html.ActionLink("process.ProcessName", "Details", new { id = process.Id })

But I don't know how to change the process.ProcessName to get the list and then I can click on. If I change it this way this just does not work out. Then the whole line does not run.

<div>
    <h1>List</h1>
    <ul>
        @foreach (var process in this.Model)
        {
            <li> 
                @Html.ActionLink(@process.ProcessName, "Details", new { id = process.Id })
            </li>
        }
    </ul> 
</div>

Solution

  • This is because the list is stored in the VewData object while in the foreach loop use the Model object.

    The following code appears to be working

    ProcessController:

    namespace MVCDemo.Controllers
    {
        public class ProcessController : Controller
        {
            // GET Process
            public ViewResult List()
            {
                var processList = from p in Process.GetProcesses()
                                  select p;
                                  
                return View(processList.ToList());
            }
    
            // GET Process/Details/5
            public ActionResult Details(int id)
            {
                var process = (from p in Process.GetProcesses()
                               where p.Id == id
                               select p).Single();
                return View(process);
            }
        }
    }
    

    List.cshtml view:

    @model List<process>
    
    <div>
        <h1>List</h1>
        <ul>
            @foreach (var process in this.Model)
            {
                <li> 
                    @Html.ActionLink(process.ProcessName, "Details", new { id = process.Id })
                </li>
            }
        </ul> 
    </div>