Reputation: 4461
I have a page that has form. I have a window.setTimeout that will call a function that trigger the submit button. The timeout is set to 10 seconds. However if the time has elapsed the function is called but the form is not submitted. It seems the submit button is not submitting the form. Please help how to make this function work. I want that if the function "SubmitForm" is called it will submit the button without clicking the submit button. Below is my code.
HTML
@model MVCViewState.Models.Product
<!DOCTYPE html>
<html>
<head>
<title>Create</title>
<script type="text/javascript">
window.setTimeout(SubmitForm, 10000);
function SubmitForm() {
alert('called');
$('#btnSubmit').submit();
}
</script>
</head>
<body>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm("Create", "Products", FormMethod.Post, new { id = "ProductForm" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Product</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<p>
<input type="submit" value="Create" id="btnSubmit"/>
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>
Controller
// // POST: /Products/Create
[HttpPost]
public ActionResult Create(Product product)
{
try
{
List<Product> products;
if (Session["products"] != null) products = Session["products"] as List<Product>;
else products = new List<Product>();
if (products != null) products.Add(product);
product.Id = products.Max(x => x.Id) + 1;
Session["products"] = products;
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Upvotes: 0
Views: 3411
Reputation: 1038710
#btnSubmit
is your submit button, not your form. It makes very little sense to attempt to submit a button. You probably meant:
$('#ProductForm').submit();
Upvotes: 3