Asp.net mvc 编辑URL不包含Id
我正在学习MVCAsp.net mvc 编辑URL不包含Id,asp.net-mvc,html-helper,asp.net-mvc-controller,Asp.net Mvc,Html Helper,Asp.net Mvc Controller,我正在学习MVC 我正在执行基本编辑操作。索引页显示以下数据 Emp_id Emp_name Emp_Sal 1 name1 sal1 Edit | Details | Delete …当我单击编辑时..URL显示如下 "http://localhost/MvcApplication1/Employee/Edit"` …但根据教程,它应该是 http://localhost/MvcApplication1/Employee/Edit/01
我正在执行基本编辑操作。索引页显示以下数据
Emp_id Emp_name Emp_Sal
1 name1 sal1 Edit | Details | Delete
…当我单击编辑时..URL显示如下
"http://localhost/MvcApplication1/Employee/Edit"`
…但根据教程,它应该是
http://localhost/MvcApplication1/Employee/Edit/01
路线图是
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
到目前为止,我还没有创建编辑操作方法
索引视图代码为:
@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>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
@ActionLink(“新建”、“创建”)
@DisplayNameFor(model=>model.Emp\u id)
@DisplayNameFor(model=>model.Emp\u name)
@DisplayNameFor(model=>model.Emp_-Sal)
@foreach(模型中的var项目)
{
@DisplayFor(modeleItem=>item.Emp\u id)
@DisplayFor(modeleItem=>item.Emp_name)
@DisplayFor(modeleItem=>item.Emp_-Sal)
@ActionLink(“编辑”,“编辑”,新的{/*id=item.PrimaryKey*/})|
@ActionLink(“详细信息”,“详细信息”,新的{/*id=item.PrimaryKey*/})|
@ActionLink(“删除”,“删除”,新的{/*id=item.PrimaryKey*/})
}
如果我遗漏了一些内容,请建议您的
ActionLink
调用没有传递正确的路由值。编辑、详细信息和删除操作需要一个id
参数作为路由值传递。假设Emp\u id
是要使用的id值,则可以按如下方式执行此操作:
@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 })
在您的示例中,您对这些值进行了注释,因此它们不会作为路由值传递,因此不会生成正确的路由。这是正确的
@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 })
它对我有用。只需删除注释并添加代码/*id=item.PrimaryKey*/为什么要添加注释是。。这就是重点。视频教程中没有取消注释。@user3102072您是否尝试过在自己的代码上复制我的代码?该错误消息似乎假定您仅尝试取消注释
/*id=item.PrimaryKey*/
部分,但您还应该将item.PrimaryKey
更改为item.Emp\u id
。