这是我的控制器操作:
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/form
@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创建动作。当相应的获取操作具有路由属性时,我可以提交邮局吗?
update
在下面看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
}
在我的查看页面上..要获取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动作中删除路由属性时,左下角的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"})
应该映射到
GET http://localhost:xxxxx/CreateDocument/4
ASP.NET MVC 5