ASP.NET表单未提交

时间:2019-06-04 12:21:26

标签: javascript c# html asp.net

当我单击“提交”时,页面将刷新,看起来一切都完成了,但是没有达到我要发送给它的ActionResult。

我尝试使用JavaScript正常提交,但都无法正常工作。

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

控制器方法:

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

这是RouteConfig:

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()

2 个答案:

答案 0 :(得分:1)

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

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

            }

答案 1 :(得分:0)

尝试

<form action="/Default/Submit" method="post">
<input type="submit">
</form>

您希望调用的ActionMethod是动词“ GET”,而表单提交方法类型是“ POST” 添加HttpPost属性可以正常工作

 [HttpPost]
Public ActionResult Submit()
{
}