我正尝试将包含两个参数的Ajax帖子发送到mvc控制器。
第一个参数:序列化模型(文件夹)
第二个参数:数组(句点)
问题是控制器在第一个参数中接收到空值:
JavaScript代码:
var data = JSON.stringify({
Folder: $('#Folder').serialize(),
Periods: periodsArr
});
function save(data) {
return $.ajax({
type: 'POST',
//cache: false,
contentType: 'application/json', //Without folder not empty and period is empty
//traditional: true,
//dataType: "json",
url: '@Url.Action("AddOrEditFolder", "Folder")',
data: data,
success: function (result) {
alert(result);
location.reload();
},
error: function () {
alert("Error!" + Error);
}
});
}
HTML代码:
<div class="card-body">
@using (Html.BeginForm(null, null, new { @id = string.Empty }, FormMethod.Post, new { @id = "Folder" }))
{
@Html.HiddenFor(model => model.FolderID)
<div class="row">
<label for="FirstNameAR" class="col-sm-2 control-label">الإسم الشخصي</label>
<div class="col-sm-4">
@Html.EditorFor(model => model.Trainee.FirstNameAR, new { htmlAttributes = new { @id= "FirstNameAr", @class = "form-control" } })
</div>
<label for="FirstNameFR" class="col-sm-2 control-label">الإسم العائلي</label>
<div class="col-sm-4">
@Html.EditorFor(model => model.Trainee.FirstNameFR, new { htmlAttributes = new { @id = "FirstNameFr", @class = "form-control" } })
</div>
</div> <br />
控制器代码:
[HttpPost]
//[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddOrEditFolder(Folder Folder, Period[] Periods)
{
//Folder Folder = JsonConvert.DeserializeObject<Folder>(Obj);
using (InternshipsEntities dbContext = new InternshipsEntities())
{
//Some code here
}
}
我该如何解决这个问题?
答案 0 :(得分:0)
使用以下代码创建数据对象以传递Ajax
请求。
var Folder = { };
$.each($('#Folder').serializeArray(), function() {
Folder[this.name] = this.value;
});
var data = {
Folder: Folder
Periods: periodsArr
};