使用RouteAttribute时无法提交表单,提交错误操作

时间:2018-04-23 15:14:27

标签: c# asp.net-mvc

以下是我的控制器操作:

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);
}

查看/表格

@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>
}

调试时,在提交时,我被重定向到带有route属性的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
}

在我的视图页面..要获取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属性时,左下角的网址显示为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 }
        );
    }
}

1 个答案:

答案 0 :(得分:4)

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

如果在控制器上使用属性路由,则需要全押。使用多个操作时,还必须包含message_deny以进一步区分操作路径。

[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"})

参考Attribute Routing in ASP.NET MVC 5