提问人:Marduk 提问时间:11/3/2023 更新时间:11/3/2023 访问量:18
序列化jquery中的所有表单,以将数据发送到表示者列表中的控制器
serialize all forms in jquery, to send data to Controller in a List of presenter
问:
我有 n 个 id 为“form-value[email protected]”的表单,在像这样的 cshtml 部分循环中生成
<div class="container-principal">
@for(val c in @Model.what)
{
@Html.Partial("Form/_Forms", Model.formPresenter);
}
<button class="icon">Go</button>
</div>
//Partial
<div id="@model.id">
<form id="[email protected]" method="post">
<div class="col-xs-3">
<div class="container">
<label> Model
<span>
@Html.TextBoxFor(model => model.Desc)
</span>
</label>
</div>
@Html.CheckBoxFor(model => model.go)
@Html.HiddenFor(x => x.id)
</div>
</form>
</div>
这是 javascript 中由 .icon 按钮调用的方法
function go() {
var myForms = document.querySelectorAll('[id^="form-value-"]');
var formSelector = ["#sdd-data", "#form-payment-methods", myForms];
var data = $("#sdd-data").serialize() + "&" + $("#form-payment-methods").serialize() + "&" + $(myForms).serialize();
validatedMultiFormElab({
formSelector: formSelector,
data: data,
url: creaUrl("Go", "Check"),
onSuccessFunction: function (response) {
//some code
},
onErrorFunction: function (data) {
hideModal('#waitmodal');
}
});
}
function validatedMultiFormElab(parameters) {
var input = {
url: parameters.url,
method: "POST",
data: parameters.data,
success: function (result) {
if (result.technicalError) {
technicalError(result);
}
else if (result.success) {
if (typeof onPreSuccessFunction != "undefined" && !parameters.skipPreSuccessFunction) {
onPreSuccessFunction(result);
}
jQuery.each(parameters.formSelector, function (index, singleFormSelector) {
if (singleFormSelector) {
cleanErrors(singleFormSelector);
}
});
if (parameters.onSuccessFunction != undefined) {
parameters.onSuccessFunction(result.outcome);
}
}
else {
mapValidationMultiFormResults(result);
if (parameters.onErrorFunction != undefined) {
parameters.onErrorFunction(result.outcome);
}
}
},
error: function (xhr, status, err) {
if (xhr.responseJSON && xhr.responseJSON.technicalError)
technicalError(xhr.responseJSON);
else
technicalError();
},
};
if (parameters.usaFormData) {
input.contentType = false;
input.processData = false;
}
return $.ajax(input);
}
最后是 Controller 类中的动作
public async Task<ActionResult> Go(PaymentOwnerDataVM model, List<PaymentMethodVM> payments, List<FormDataVM> formPresenter)
{
//some code
}
模型总是可以的(是一个单一的表单),payments是一个循环中生成的多输入的表单,也许是空的,但没问题,formPresenter(呈现的序列化我的表单输入的位置)总是空的,我错了哪里?
答: 暂无答案
评论