【发布时间】:2021-03-26 12:21:19
【问题描述】:
我有一个在部分视图中显示的记录表,其中一些没有 ID 值。因此,在单击特定记录的编辑链接时,我尝试使用替代字段作为 ID。我不确定我是否可以合法地拥有两个 Post 操作方法,即使我使用不同的方法名称和参数。
目前,如果我单击带有 ID 的记录,则会调用正确的操作方法。如果我选择没有 ID 的记录(而不是使用唯一的“帐户”字符串 ID),我会得到 404。
RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
局部视图:
...
<td>
@if (item.ID != null)
{
@Html.ActionLink("Edit", "EditBudget", new { id = item.ID })
}
else if (item.Account != null)
{
@Html.ActionLink("Edit", "EditAccountBudget", new { account = item.Account })
}
</td>
预算控制器:
// POST: Budgets/Edit/5
[Route("edit/{id?}")]
[HttpPost]
public ActionResult EditBudget(int? id = null, FormCollection collection = null)
{
...
// Responding correctly for URL: http://localhost:4007/edit/19
}
[Route("editaccountbudget/{account}")]
[HttpPost]
public ActionResult EditAccountBudget(string account)
{
...
// Getting 404 for URL: http://localhost:4007/editaccountbudget/6000..130
}
【问题讨论】:
-
[Route("budgets/与@Html.ActionLink("Edit"不兼容,我猜。 -
已更新。仍然得到 404。
-
试试这个
[Route("edit/editaccountbudget/{account}")] -
@IrishChieftain:您似乎没有在这里使用帖子。尝试删除
[HttpPost]属性。
标签: c# asp.net asp.net-mvc model-view-controller asp.net-mvc-5