I'm working on an MVC project where I have several buttons posting but with a separate value so the controller know what to do as shown below.
<input type="submit" value="Reject" name="rejectHighlight" class="icon-cancel-1 btn btn-danger" />
<input type="submit" value="Archive" name="archiveHighlight" class="icon-cancel-1 btn btn-danger" />
This was all working perfectly until I added the following javascript code to handle double clicks.
$(document).on('submit', 'form', function () {
var buttons = $(this).find('[type="submit"]');
if ($(this).valid()) {
buttons.each(function (btn) {
$(buttons[btn]).prop('disabled', true);
});
}
else {
buttons.each(function (btn) {
$(buttons[btn]).prop('disabled', false);
});
}
});
Now the JavaScript code works perfectly and I'm not able to double click but I've now lost my button values when it gets posted to the controller.
I'm not experienced with JavaScript and and have not been able to find an answer to correct this in my searches. Can someone please point me in the right direction on how to include the values on submit again?
Updated to show the view and controller method as shown below.
Below is the view
@model OperationHighlights.Models.Highlight
@{
ViewBag.Title = "Edit Highlight";
}
<h2>Edit Highlight</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
@if (ViewBag.Message != null)
{
<ul>
<li class="text-success">@Html.Raw(ViewBag.Message)</li>
</ul>
}
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.OrigGroupId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CurrGroupId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CurrGroupId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ClassificationId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ClassificationId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ClassificationId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Description, 5, 100, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<hr />
@if (ViewBag.SubmitReview == "Review")
{
<div class="form-group">
@Html.LabelFor(model => model.Comments, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Comments, 5, 100, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
<input type="submit" value="Approve" name="submitHighlight" class="icon-check btn btn-default" />
<input type="submit" value="Reject" name="rejectHighlight" class="icon-cancel-1 btn btn-danger" />
<input type="submit" value="Archive" name="archiveHighlight" class="icon-cancel-1 btn btn-danger" />
</div>
</div>
}
else
{
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
<input type="submit" value="Submit" name="submitHighlight" class="btn btn-default" />
</div>
</div>
}
</div>
}
Below is the controller method
[HttpPost, ActionName("Edit")]
[ValidateAntiForgeryToken]
public ActionResult EditPost(int? id, string submitHighlight, string rejectHighlight, string archiveHighlight)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Models.Highlight highlightToUpdate = db.Highlights.Find(id);
if (TryUpdateModel(highlightToUpdate, "", new string[] { "Title", "Description", "ClassificationId", "CurrGroupId", "Comments" }))
{
try
{
if (!string.IsNullOrEmpty(submitHighlight))
{
// Do some work here
}
if (!string.IsNullOrEmpty(rejectHighlight))
{
// Do some work here
}
if (!string.IsNullOrEmpty(archiveHighlight))
{
// Do some work here
}
db.SaveChanges();
}
catch (DataException dex)
{
// Log the error
ExceptionHandling.CreateDataErrorLog(dex, 1);
// Alert the user
ModelState.AddModelError("", "Unable to edit highlight. Please try again, and if the problem persists see your system administrator.");
}
}
// Redirect Logic Here
}