C# 使用RouteAttribute时无法提交表单,提交操作错误
以下是我的控制器操作: HttpGetC# 使用RouteAttribute时无法提交表单,提交操作错误,c#,asp.net-mvc,C#,Asp.net Mvc,以下是我的控制器操作: HttpGet // GET: ControllerName/Create [Route("CreateDocument/{personId}")] public ActionResult Create(int personId) { var personDocumentation = new PersonDocumentation() { PersonId = pilotId }; ViewBag.Documentatio
// 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]
[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);
}
HttpPost
// 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]
[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);
}
查看/表单
@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操作,我有一个按钮链接:
@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 }
);
}
}
您正在混合基于属性和约定的路由 如果在控制器上使用属性路由,则需要全力以赴。当使用多个动作时,还必须包括
[Http{Verb}]
,以进一步区分动作路由
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
应用于控制器
所以现在你打电话的时候
@Html.BeginForm("Create", "PersonDocumentations", FormMethod.Post, new {enctype = "multipart/form-data" }))
在PersonDocumentationsController.Create
视图中,它将映射到正确的操作
POST-CreateDocument/4
对于操作链接,还需要包括所需的控制器
@Html.ActionLink("Create New Documentation", "Create", "PersonDocumentations" , new {personId = Model.PersonId}, new {@class = "btn btn-info"})
应该映射到哪个
GEThttp://localhost:xxxxx/CreateDocument/4
参考浏览器的网络是怎么说的,您实际发送了哪个HTTP方法?它说的是POST,但当我调试时,我会转到HttpGet
POST/CreateDocument/4
您是否尝试添加[HttpGet]动词以获取操作并查看?@user9405863我有,但随后它导致404个错误,说找不到CreateDocument/4