gpt4 book ai didi

c# - 使用 RouteAttribute 时无法提交表单,提交到错误的 Action

转载 作者:太空狗 更新时间:2023-10-30 01:13:31 26 4
gpt4 key购买 nike

这是我的 Controller 操作:

HttpGet

// GET: ControllerName/Create
[Route("CreateDocument/{personId}")]
public ActionResult Create(int personId)
{
var personDocumentation = new PersonDocumentation()
{
PersonId = pilotId
};
ViewBag.DocumentationTypeIdSelection = new SelectList(db.DocumentationTypes, "Id", "DocumentationTypeName");

return View(personDocumentation);
}

HttpPost

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,PersonId,DateReceived,DocumentationTypeId,Filepath")] PersonDocumentation personDocumentation)
{
if (ModelState.IsValid)
{
if (Request.Files.Count > 0)
{
// performing stuff here
}

db.PersonDocumentations.Add(personDocumentation);
db.SaveChanges();
return RedirectToAction("Index", "PersonDocumentations", new {pilotId = personDocumentation.PilotId});
}

ViewBag.DocumentationTypeId = new SelectList(db.DocumentationTypes, "Id", "DocumentationTypeName", personDocumentation.DocumentationTypeId);
return View(personDocumentation);
}

View /表单

@using (Html.BeginForm("Create", "PersonDocumentations", FormMethod.Post, new {enctype = "multipart/form-data" }))
{
// Form stuff here

<div class="form-group">
<input type="submit" value="Create" class="btn btn-lg btn-success" />
</div>
}

调试时,在提交时我被重定向到具有路由属性的 HttpGet Create 操作。当相应的 Get 操作具有路由属性时,我可以提交到 Post Action 吗?

更新

在看了下面的 Nkosi 回答之后..我有这个:

[HttpGet]
[Route("CreateDocument/{personId}")]
public ActionResult Create(int personId)
{
// stuff here
}

[HttpPost]
[Route("CreateDocument")]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,PersonId,DateReceived,DocumentationTypeId,Filepath")] PersonDocumentation personDocumentation)
{
// stuff here
}

在我的 View 页面上.. 要获得 HttpGet 操作,我有一个按钮链接:

@Html.ActionLink("Create New Documentation", "Create", new {personId = Model.PersonId}, new {@class = "btn btn-info"})

但是,当我将鼠标悬停在按钮上时,我会在左下角看到链接:

http://localhost:xxxxx/CreateDocument?personId=4

不应该是:

http://localhost:xxxxx/CreateDocument/4

当我从 HttpPost 操作中删除 Route 属性时,左下角的 url 显示为 http://localhost:xxxxx/CreateDocument/4

然后当我点击按钮时我收到 404 错误:

Requested Url:  /CreateDocument


public class 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 = "PersonInfo", action = "Index", id = UrlParameter.Optional }
);
}
}

最佳答案

您正在混合属性和基于约定的路由。

如果在 Controller 上使用属性路由,你需要全力以赴。当使用多个 Action 时,你还必须包含 [Http{Verb}] 以进一步区分 Action 路由。

public class PersonDocumentationsController : Controller {

[HttpGet]
public ActionResult Index() {
//...
}

//GET CreateDocument/4
[HttpGet]
[Route("CreateDocument/{personId:int}")]
public ActionResult Create(int personId) {
//...
}

//POST CreateDocument/4
[HttpPost]
[Route("CreateDocument/{personId:int}")]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,PersonId,DateReceived,DocumentationTypeId,Filepath")] PersonDocumentation personDocumentation) {
//...
}
}

这还假设没有 RoutePrefix 应用于 Controller 。

所以现在当你打电话的时候

@Html.BeginForm("Create", "PersonDocumentations", FormMethod.Post, new {enctype = "multipart/form-data" }))

PersonDocumentationsController.Create View 中,它将映射到正确的操作。

POST CreateDocument/4

对于操作链接,您还需要包含所需的 Controller

@Html.ActionLink("Create New Documentation", "Create", "PersonDocumentations" , new {personId = Model.PersonId}, new {@class = "btn btn-info"})

应该映射到

GET http://localhost:xxxxx/CreateDocument/4

引用 Attribute Routing in ASP.NET MVC 5

关于c# - 使用 RouteAttribute 时无法提交表单,提交到错误的 Action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49984443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com