Question

I have the following folder structure: Controller/EntityController Views/Entity/Index.cshtml Views/Shared/DisplaTemplates/CustomEntity.cshtml

EntityController:

public ActionResult Index()
{
    var entities = CustomEntityRepository.GetAll();
    return View(entities);
}

public ActionResult Edit(int id)
{
    if (id == 0)
    {
        return HttpNotFound();
    }
    var entity = CustomEntityRepository.Find(e => e.ID == id);
    return View(entity);
}

Contens of Index.cshtml:

@model IEnumerable<CustomEntity>

@{
    ViewBag.Title = "Index";
}
@Html.DisplayForModel()

Contents of CustomEntity.cshtml:

@model CustomEntity
<div>
    Entity: 
    <strong>@Model.Name</strong>
    @Html.ActionLink("Show All","Edit","Entity", new { id= @Model.ID}, null)
    @Html.LabelFor(m=>m.Icon):
    @Model.Icon
    @Html.LabelFor(m=>m.TypeName):
    @Model.TypeName
</div>

EDIT: Routing config:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

However, the ActionLink doesn't work in the way I expect. I want the ActionLink to point to EntityController Edit action with the ID supplied by each individual entity. Instead, it renders the following url:

http://localhost/MySite/Reports?action=Edit&controller=Entity&id=1

I don't know where /Reports/ is coming from.

Thanks!

Was it helpful?

Solution

Use Html.RouteLink specifying route name

@Html.RouteLink("Show All", "Default", new { Controller = "Entity", Action = "Edit", id = Model.Id })

Other option is to change your map routes order, default route should be at end of your route config file

Regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top