C# 从GET发布到不同的操作名称
我有一个显示用户配置文件的控制器。显示配置文件的操作就在索引下:its/user/123以显示用户123。在路线图上设置如下:C# 从GET发布到不同的操作名称,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有一个显示用户配置文件的控制器。显示配置文件的操作就在索引下:its/user/123以显示用户123。在路线图上设置如下: routes.MapRoute( name: "User", url: "User/{id}", defaults: new {controller = "User", action = "Index", id = UrlParameter.Optional} ); pub
routes.MapRoute(
name: "User",
url: "User/{id}",
defaults: new {controller = "User", action = "Index", id = UrlParameter.Optional}
);
public class UserController : Controller
{
[Authorize]
public ActionResult Index(int id)
{
*edited out for simplicity*
return View(model);
}
[Authorize]
[HttpPost]
public ActionResult Follow(int id)
{
*edited out for simplicity*
return RedirectToAction("Index", "User", new { id });
}
}
现在我的控制器看起来像这样:
routes.MapRoute(
name: "User",
url: "User/{id}",
defaults: new {controller = "User", action = "Index", id = UrlParameter.Optional}
);
public class UserController : Controller
{
[Authorize]
public ActionResult Index(int id)
{
*edited out for simplicity*
return View(model);
}
[Authorize]
[HttpPost]
public ActionResult Follow(int id)
{
*edited out for simplicity*
return RedirectToAction("Index", "User", new { id });
}
}
和我的网络表单:
using (Html.BeginForm("Follow", "User", FormMethod.Post))
{
<input type="submit" value="Follow!" class="btn btn-success" />
}
使用(Html.BeginForm(“Follow”、“User”、FormMethod.Post))
{
}
现在GET可以很好地工作,但是当我提交帖子时,它并没有从url/user/123传递用户id
如果我要从一个入门索引转到一个后续帖子,以便它交出{id},我需要在
Html.BeginForm
中执行额外的操作吗?您可以使用BeginForm
重载,该重载使用routevalue
并使用RequestContext
从Url传递id
Html.BeginForm("Follow","User", new { id = @Url.RequestContext.RouteData.Values["id"] }, FormMethod.Post);
与论坛网站不同,我们不使用“感谢”或“感谢任何帮助”或签名。顺便说一句,这是“提前感谢”,而不是“提前感谢”。