gpt4 book ai didi

javascript - ASP.NET 表单不提交

转载 作者:行者123 更新时间:2023-11-30 19:30:50 27 4
gpt4 key购买 nike

当我点击提交时,页面被刷新,看起来一切都已完成,但没有到达我试图将其发送到的 ActionResult。

我试过正常提交和使用 JavaScript,但都不起作用。

<form method="post" autocomplete="off" asp-controller="Default" asp-action="Submit">
<button id="SubmitButton" type="submit">Submit</button>
</form>

Controller 方法:

namespace (Removed).Controllers
{
public class DefaultController : Controller
{
[HttpPost]
public ActionResult Submit()
{
DBController1 DB1 = new DBController1();
AlertManagement am = new AlertManagement();

am.FirstName = Request.Form["FirstName"];
am.LastName = Request.Form["LastName"];
am.Email = Request.Form["EmailAddress"];
am.Mobile = Request.Form["PhoneNumber"].Replace("(", "").Replace(")", "").Replace(" ", "").Replace("-", "");
am.Affiliation = Request.Form["Affiliation"];
am.StartDate = Convert.ToDateTime(Request.Form["StartDate"]).Date;
am.EndDate = Convert.ToDateTime(Request.Form["EndDate"]).Date;

DB1.AlertManagement.Add(am);
DB1.SaveChanges();

return RedirectToAction("Index");
}

public ActionResult Index()
{
return View();
}
}
}

这是路由配置:

namespace (Removed)
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Index",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
name: "Submit",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Default", action = "Submit", id = UrlParameter.Optional }
);
}
}
}

当我点击提交时,它总是转到 ActionResult Index()

最佳答案

这里是在黑暗中拍摄的,但是您的 Controller 看起来像这样吗?

public class DefaultController: Controller
{
[HttpPost] // This attribute states that this action result can only be accessed when using http POST verb.
public IActionResult Submit()
{
return RedirectToAction("index", "home");
}

}

此外,您发布的是什么?如果您在示例中故意遗漏了这些字段,请确保在您的 POST 方法中有这个。

<form method="post" autocomplete="off" asp-controller="Default" asp-action="Submit">
<input type="text" name="nameOfElement" id="clientName" />
<button id="SubmitButton" type="submit">Submit</button>
</form>
public class DefaultController: Controller
{
[HttpPost]
public IActionResult Submit([FromForm] string nameOfElement)
{
return RedirectToAction("index", "home");
}

}

关于javascript - ASP.NET 表单不提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56443798/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com