0
如何以某種方式在JavaScript中使用變量擴展formdata,以便在控制器POST操作中可用。如何使用JavaScript擴展formdata並將其發佈到控制器
所以我想要做的是,添加一個變量更新(這是一個布爾),並將這與我的formdata發送到控制器。
這裏是我的.js的代碼文件&控制器代碼
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>" });
}
編輯: 這的確是一個問題重複,道歉說。我通過這裏提供的答案之一來修復它:jquery form.serialize and other parameters
所以我創建了一個變量並添加了$ .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)
{
}
嘗試回答這些問題的一個http://stackoverflow.com/questions/10398783/jquery-form-serialize-and-other-parameters – JamieD77