【问题标题】:Action routing goes to same controller动作路由到同一个控制器
【发布时间】:2016-05-02 13:57:57
【问题描述】:

我正在尝试使用 HTTPPost 操作链接到不同的控制器,但是,当我尝试时,它只是将我的路由值附加到当前页面的控制器上。例如,如果我尝试使用表单和 HTTPPOSTSite/ViewIndex 链接到 Page/createPage,那么它会抛出 404 并说它无法访问 Site/Page/createPage。为什么会这样?我该如何阻止它?

这是我的网站/createPage:

public ActionResult createPage(int siteId, string title, string description, bool isBlog = false)
        {
            if (string.IsNullOrWhiteSpace(title) ||
                string.IsNullOrWhiteSpace(description))
            {
                return RedirectToAction("ViewIndex", new { siteId = siteId, message = "Please fill out all fields" });
            }
            try
            {
                Ops.PageOps.createPage(title, description, siteId, isBlog);
                return RedirectToAction("ViewIndex", "Site", new { siteId = siteId, message = "Page created!" });
            }
            catch (Exception e)
            {
                return RedirectToAction("ViewIndex", new { siteId = siteId, message = "Error occured: " + e.Message });
            }
        }

这是我的表格:

<form method="post" action="Page/createPage">
    <input class="form-field form-control" type="text" name="title" placeholder="Page Title" />
    <input class="form-field form-control" type="text" name="description" placeholder="Page Description" />
    <input class="form-field form-control" type="hidden" name="siteId" value="@site.Id" />
    Blog page? <input class="form-field" type="checkbox" value="true" name="isBlog" /><br /><br />
    <input class="btn btn-info" type="submit" value="Create" /> 
</form>

我怀疑这是否相关,但这是我的站点控制器:

public class SiteController : Controller
    {
        /// <summary>
        /// The create page
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateIndex()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(string title, string description, bool privateSite = false)
        {
            Ops.SiteOps.createSite(Authenticated.AuthenticatedAs, title, description, privateSite);

            return RedirectToAction("Index", "Home");
        }

        public ActionResult ViewIndex(int siteId, string message = null)
        {
            ViewBag.message = message;
            ViewBag.siteId = siteId;
            return View();
        }


    }

【问题讨论】:

  • 你是如何链接的?可以显示代码吗?
  • @Shyju 请查看更新后的问题

标签: html asp.net-mvc post razor routing


【解决方案1】:

使用Html.BeginForm 辅助方法来呈现您的表单标签。这将在表单的 action 属性中呈现正确的 HttpPost 操作相对路径。

@using(Html.BeginForm("CreatePage","Page"))
{
    <input class="form-field form-control" type="text" name="title" placeholder="Title" />
    <input class="form-field form-control" type="text" name="description" " />
    <input class="form-field form-control" type="hidden" name="siteId" value="@site.Id" />
    Blog page? <input class="form-field" type="checkbox" value="true" name="isBlog" />
    <input class="btn btn-info" type="submit" value="Create" /> 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2019-10-26
    • 2014-08-26
    • 2014-08-07
    • 1970-01-01
    相关资源
    最近更新 更多