0

How can I somehow extend the formdata in Javascript with a variable so this will be available in my controller POST action.

So what i want to do is, add a variable update (which is a bool) and send this with my formdata to the controller.

Here's the code of my .js file & controller code

    bootbox.confirm("Are you sure you want to override this Quotation?", function (result) {
        if (result === null) {
           //do nothing
        } else if (result) {
           if (!$form[0].checkValidity()) {
               return false;
           }

           var btn = $('#saveQuotationForm');
           btn.attr('onclick', '');
           btn.attr('class', 'glyphicon glyphicon-floppy-save');
           btn.text('Saving...');

           var formurl = $form.attr('action');
           var formdata = $form.serialize();

           console.log(formurl + " =formurl");
           console.log(formdata + " =formdata");

           //tricky part, i want to do something like this:
           //var update = new boolean(false);
           //formdata.append(update, true); --> but this is obviously not the way to go :), anyone got idea's?**

           $.post(formurl, formdata, function (data) {
                if (data && data.State === 'success') {
                    btn.attr('class', 'glyphicon glyphicon-floppy-saved');
                    btn.text('Saved');
                    $('#placeholderAlert').append('<div class="alert alert-success" role="alert"><strong>Saved</strong> ' + data.Message + '</div>');
                } else {
                    btn.attr('class', 'glyphicon glyphicon-floppy-remove');
                    btn.text('Saving failed');
                }
          });
       }
  });




    [HttpPost]
    [ValidateAntiForgeryToken]
    public JsonNetResult Products_Quotation_Save(vmProducts_Quotation_Save quotation)
    {
        quotation.DateCreated = DateTime.Now;
        QuotationCache.Instance.SaveQuotation(GetClientCode(HttpContext), GetUserName(HttpContext), quotation, MuseContext);
        return JsonNet(new { State = "success", Message = "Quotation '" + quotation.Name + "' has been saved with reference " + quotation.ShortCode + ". <a href=" + Url.Action(RouteItemAction.ResaPlus_Quotations) + " class=\"alert-link\">Go to overview</a>" });
    }

EDIT: This is indeed a duplicate of another question, apologies for that. I fixed it by one of the answers provided here: jquery form.serialize and other parameters

so I created a variable and added it with $.param, then in my controller I have access to that param.

var update = { 'update': true };
var formurl = $form.attr('action');
var formdata = $form.serialize() + '&' + $.param(update);

$.post(formurl, formdata, function (data) {

});

[HttpPost]
[ValidateAntiForgeryToken]
public JsonNetResult Products_Quotation_Save(vmProducts_Quotation_Save quotation, bool update)
{
}
Community
  • 1
  • 1
Dylan Slabbinck
  • 846
  • 1
  • 16
  • 27
  • 1
    try one of these answers http://stackoverflow.com/questions/10398783/jquery-form-serialize-and-other-parameters – JamieD77 Sep 30 '15 at 16:01

1 Answers1

0

you can use serializeArray() to create an update-able object. http://api.jquery.com/serializeArray/

var formdata = $form.serializeArray();
formdata.push({name: 'update', value: true});

$.post(formurl, formdata, function (data)
JamieD77
  • 13,796
  • 1
  • 17
  • 27