创建主页链接(.Net、MVC)
我有一个 .Net MVC 3 应用程序,并有一个名为SupplierController 的控制器,它管理两个视图(页面)、一个供应商列表和一个编辑视图。 在列表视图中,我创建了如下编辑链接:
@model IEnumerable<RyfMvcTestApplication1.Models.DataModel.Model.Supplier>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id })
</td>
</tr>
}
在编辑视图中,自动创建了一个反向链接,
<div>
@Html.ActionLink("Back", "Index")
</div>
但这会导致根页面(http://localhost)。我想获取列表页面,该页面的 url 为 http://localhost/Supplier。到目前为止,我做得最好的是,
@Html.ActionLink("Back", "Index/Supplier")
但这会导致 url http://localhost/Supplier/Index/Supplier,这不是我想要的。
我的供应商控制器:
public class SupplierController : Controller
{
public ActionResult Index()
{
List<Supplier> suppliers = sr.GetAll();
return View("List", suppliers);
}
}
I have a .Net MVC 3-Application and have a controller named SupplierController which administers two views (pages), a list and an edit view for suppliers.
In the list view, I created Edit-links like this:
@model IEnumerable<RyfMvcTestApplication1.Models.DataModel.Model.Supplier>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id })
</td>
</tr>
}
In the edit view, a back link was automatically created
<div>
@Html.ActionLink("Back", "Index")
</div>
but this leads to the root page (http://localhost). I would like to get the list page, which has to the url http://localhost/Supplier. The best I worked out so far is
@Html.ActionLink("Back", "Index/Supplier")
but this leads to the url http://localhost/Supplier/Index/Supplier, which is not what I want.
My SupplierController:
public class SupplierController : Controller
{
public ActionResult Index()
{
List<Supplier> suppliers = sr.GetAll();
return View("List", suppliers);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用以下重载来指定操作和控制器:
You could use the following overload allowing you to specify the action and the controller:
问题出在我的路由配置中。它看起来像这样:
如果我将 MapRoute 方法中的默认控制器更改为其他控制器,则一切正常。
我不知道为什么这会成为一个问题,并感谢您的评论。
The problem was in my routing configuration. It looked like this:
If I change the default controller in the MapRoute-method to something else, all works fine.
I don´t know why this should be a problem and would be thankful for comments.