我正在尝试序列化一个可以包含对象数组的表单并将其发送到我的应用程序服务,但我找不到任何使其工作的方法......
这是我的保存方法
var brewer = _$form.serializeFormToObject();
abp.services.app.brewer.create(brewer).done(() => {
...
});
// brewerService
serviceNamespace.create = function(brewerDto, ajaxParams) {
return abp.ajax($.extend({
url: abp.appPath + 'api/services/app/brewer/Create',
type: 'POST',
data: JSON.stringify(brewerDto)
}, ajaxParams));
};
这是我的表格
<form name="AddBrewerForm" role="form" novalidate class="form-validation">
<ul class="nav nav-tabs tab-nav-right" role="tablist">
<li role="presentation" class="active">
<a href="#brewer-detail" data-toggle="tab">@L("BrewerDetails")</a>
</li>
<li role="presentation">
<a href="#beers-list" data-toggle="tab">@L("Beers")</a>
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane animated fadeIn active" id="brewer-detail">
<div class="row clearfix" style="margin-top:10px;">
<div class="col-sm-12">
<div class="form-group form-float">
<div class="form-line">
<input id="name" type="text" name="Name" required class="validate form-control" />
<label for="name" class="form-label">Name</label>
</div>
</div>
</div>
</div>
</div>
<div role="tabpanel" class="tab-pane animated fadeIn" id="beers-list">
<div id="beer-list-content">
<div class="brewer-beer-card row form-group form-float" style="border: 1px solid black;">
<div class="col-md-10">
<div class="form-line">
<input id="beer-0" type="text" name="Beers[0].Name" required class="validate form-control" />
<label for="beer-0" class="dynamic-beer-label form-label">@L("Name")</label>
</div>
</div>
</div>
<div class="brewer-beer-card row form-group form-float" style="border: 1px solid black;">
<div class="col-md-10">
<div class="form-line">
<input id="beer-1" type="text" name="Beers[1].Name" required class="validate form-control" />
<label for="beer-1" class="dynamic-beer-label form-label">@L("Name")</label>
</div>
</div>
</div>
</div>
<div class="row" style="margin-top: 10px;">
<div class="col-sm-12 ">
<button type="button" id="add-beer-button" class="btn btn-primary waves-effect">
@L("Add")
</button>
</div>
</div>
</div>
</div>
</form>
应用服务Dto
public class InsertBrewerDto
{
[Required]
[StringLength(128)]
public string Name { get; set; }
public List<NewBrewerBeerDto> Beers { get; set; }
}
public class NewBrewerBeerDto
{
[Required]
public string Name { get; set; }
}
用于序列化的 ABP 的 jquery 插件
$.fn.serializeFormToObject = function () {
//serialize to array
var data = $(this).serializeArray();
//add also disabled items
$(':disabled[name]', this).each(function () {
data.push({ name: this.name, value: $(this).val() });
});
//map to object
var obj = {};
data.map(function (x) { obj[x.name] = x.value; });
return obj;
};
我的啤酒数组不断收到 null ......
所以我尝试使用 $(form).serialize() 但我收到此错误:您的请求无效!
在验证期间检测到以下错误。
所以没有显示错误...
这是日志文件 WARN 2017-09-30 10:53:13,458 [31] nHandling.AbpApiExceptionFilterAttribute - 方法参数无效!有关详细信息,请参阅验证错误。Abp.Runtime.Validation.AbpValidationException:方法参数无效!有关详细信息,请参阅验证错误。在 Abp.Runtime.Validation.Interception.MethodInvocationValidator.ThrowValidationError() 在 Abp.Runtime.Validation.Interception.MethodInvocationValidator.Validate() 在 Abp.WebApi.Validation.AbpApiValidationFilter.d__5.MoveNext() --- 堆栈跟踪结束以前抛出异常的位置 --- 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Abp.WebApi.Auditing.AbpApiAuditFilter.d__4 的 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)。
Javascript 控制台错误:
{code:0,消息:“您的请求无效!”,详细信息:“验证期间检测到以下错误。↵ - ↵”,validationErrors:Array(1)}代码:0详细信息:“检测到以下错误验证期间。↵ - ↵”消息:“您的请求无效!” 验证错误:数组(1)0:成员:数组(1)0:“brewerDto”长度:1条消息:“”
编辑1
用于获取控制台的 JS
var b1 = _$form.serialize();
var b2 = _$form.serializeArray();
var b3 = _$form.serializeFormToObject();
console.log(b1);
console.log(b2);
console.log(b3);
b1
Name=123&Beers%5B0%5D.Name=123&Beers%5B1%5D.Name=321
b2
(3) [{…}, {…}, {…}]
0:{name: "Name", value: "123"}
1:{name: "Beers[0].Name", value: "123"}
2:{name: "Beers[1].Name", value: "321"}
length:3
b3
{Name: "123", Beers[0].Name: "123", Beers[1].Name: "321"}
Beers[0].Name :"123"
Beers[1].Name : "321"
Name : "123"