我正在尝试使用HTTPPost操作链接到不同的控制器,但是,当我尝试它时,只需将我的路由值附加到当前页面的控制器上。例如,如果我尝试使用表单和Site/ViewIndex
从Page/createPage
链接到HTTPPOST
,那么它会抛出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();
}
}
答案 0 :(得分: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" />
}