2

I am revisiting MVC after some time I am facing some weird errors, which I just can't seem to solve.

@Model List<StockEdgeTest.Models.Department>
@using StockEdgeTest.Models

@{
    ViewBag.Title = "Department List";
}

<div style="font-family:'Agency FB'">
    <h2>Department List</h2>

    <ul>
        @foreach (Department dep in @Model)
        {
            <li>
                @Html.ActionLink(dep.DepartmentName, "Index", "Employee", new { DepartmentID = Convert.ToInt32(dep.DepartmentID) })
            </li> 
        }

    </ul>
</div>

This View gives o/p

System.Collections.Generic.List`1[StockEdgeTest.Models.Department] List Department List HR Finance DB IT Management Corpo Bank Facilities Useless Directors

I do not know how to remove this line System.Collections.Generic.List`1[StockEdgeTest.Models.Department] List

More importantly, the actionlink does not seem to working, it just generates https://localhost:44354/?Length=8 instead of https://localhost:44354/Employee/Index

any ideas how to address this? EDIT: action

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StockEdgeTest.Models;

namespace StockEdgeTest.Controllers
{
    public class DepartmentController : Controller
    {
        // GET: Department
        public ActionResult Index()
        {
            EmployeeContext ecn = new EmployeeContext();
            List<Department> e1 = ecn.Department.ToList();
            return View(e1);
        }

    }
}
Serge
  • 40,935
  • 4
  • 18
  • 45

1 Answers1

1

fix your view :

@model List<StockEdgeTest.Models.Department>

@{
    ViewBag.Title = "Department List";
}

<div style="font-family:'Agency FB'">
    <h2>Department List</h2>

    <ul>
        @foreach (Department dep in @Model)
        {
            <li>
@Html.ActionLink(dep.DepartmentName, "Index", "Employee", new { id = dep.DepartmentID}, null )
            </li> 
        }

    </ul>
</di

and in you Index action replace:

return View()

with

[Route("{id?}")]
public ActionResult Index(int id)
{
    .....
List<StockEdgeTest.Models.Department> model= ...your code
return view(model);
}
```
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Thanks a lot. I tried your steps and it is running now, I just need a bit more clarification, Why do I need to use [Route] specifically to expose the actionlink, I remember working on mvc previously and this was not needed. – Somsubhra Dragoon Bhattacharya Feb 08 '21 at 08:59
  • It is because Index is usually a default route and doesn't have any input parameters.Since you need to use DepartmentID as your input parameter for index I put Id as a route value. But since it has "?" it is an optional route value and at the start Index will be called by default without a route value and from your link it will be called with value. – Serge Feb 08 '21 at 11:47
  • Done the upvote part. So you are right as in the default index does not have an parameter, but the is the index from department controller (and this is the path specified as default path in routeconfig), whereas the index which is called from the actionlink is from Employee controller and needs the parameter DepartmentID(not optional). I tried without the ?id part, and it still worked with just [Route]. – Somsubhra Dragoon Bhattacharya Feb 08 '21 at 14:01
  • It depends how is your routing configured in startup config file. But Id? is always more safe then id – Serge Feb 08 '21 at 14:14
  • I guess that your default route pattern is - "{controller=Home} /{action=Index} /{id?}"); Since you have {id?} here already, you don' need to put route attribute again. But I didn' t see your startup file, this was why I added route just in case if it was different. – Serge Feb 08 '21 at 14:55