I am learning MVC from
https://www.youtube.com/watch?v=ItSA19x4RU0&list=PL6n9fhu94yhVm6S8I2xd6nYz2ZORd7X2v
I am doing Basic Edit operation..Index Page shows following Data ..
Emp_id Emp_name Emp_Sal
1 name1 sal1 Edit | Details | Delete
...When i click on Edit ..URL Display Like
"http://localhost/MvcApplication1/Employee/Edit"`
...But Accordng to Tutorial it should be like
http://localhost/MvcApplication1/Employee/Edit/01
the map Route is
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I have not create Edit ActionMethod till now .
Index View Code is :
@model IEnumerable<BusinessLayer.Employee>
@{
ViewBag.Title = "Index";
}
<h2>
Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Emp_id)
</th>
<th>
@Html.DisplayNameFor(model => model.Emp_name)
</th>
<th>
@Html.DisplayNameFor(model => model.Emp_Sal)
</th>
<th>
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Emp_id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Emp_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Emp_Sal)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
Please Suggest if I am missing something
Your ActionLink
calls don't pass the correct route values. The Edit, Details and Delete actions expect an id
parameter to be passed as a route value. You can do this as follows, assuming the Emp_id
is the id value you want to use:
@Html.ActionLink("Edit", "Edit", new { id=item.Emp_id }) |
@Html.ActionLink("Details", "Details", new { id=item.Emp_id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Emp_id })
In your example, you have these values commented so they won't be passed as route values and thus no correct route will be generated.