Javascript Form.serialize()似乎返回null
我试图将一个表单发布到控制器,控制器在其中接受FormCollection作为参数。当我尝试访问dsCollection中的项目时,我得到null。FormCollection中唯一的项目是Javascript Form.serialize()似乎返回null,javascript,c#,jquery,html,asp.net-mvc-5,Javascript,C#,Jquery,Html,Asp.net Mvc 5,我试图将一个表单发布到控制器,控制器在其中接受FormCollection作为参数。当我尝试访问dsCollection中的项目时,我得到null。FormCollection中唯一的项目是“\uu RequestVerificationToken”和“dsCollection”。另一件事是,我正在使用javascript动态生成表单HTML,同时为它们赋值 以下是我将数据发布到服务器端的ajax: $(document).ready(function () { $('#postEditData
“\uu RequestVerificationToken”
和“dsCollection”
。另一件事是,我正在使用javascript动态生成表单HTML,同时为它们赋值
以下是我将数据发布到服务器端的ajax:
$(document).ready(function () {
$('#postEditDatasource').click(function (event) {
alert(JSON.stringify(deletedDatapoints));
//serialise and assign json data to hidden field
$('#dsDeletedDP').val(JSON.stringify(deletedDatapoints));
//anti forgery token
//get the form
var form = $('#__dsAjaxAntiForgeryForm');
//from the form get the antiforgerytoken
var token = $('input[name="__RequestVerificationToken"]', form).val();
var URL = '/Settings/EditDatasource';
console.log(form);
//we make an ajax call to the controller on click
//because the controller has a AntiForgeryToken attribute
//we need to get the token from the form and pass it with the ajax call.
$('#__dsAjaxAntiForgeryForm').on('submit', function () {
$.ajax({
url: URL,
data: {
__RequestVerificationToken: token,
dsCollection: form.serialize()
},
type: 'POST',
success: function (result) {
alert('this worked')
if (data.result == "Error") {
ShowDatasourcePostAlert('failPost', 3000);
} else {
ShowDatasourcePostAlert('successPost', 3000);
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR + ', ' + textStatus + ', ' + errorThrown);
}
})
return false;
})
});
})
这是我的控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditDatasource(FormCollection dsCollection)
{
var devName = dsCollection["deviceName"];
//LoadDataToViewModel();
//Get the name of the device in which we will pass it to the XML edit helper
//In order for us to locate the
//var getEditDatasource = Convert.ToString((from ds in dsVM.devices
// where ds.deviceID == Convert.ToInt64(dsCollection["dsID"])
// select ds.deviceName));
return new EmptyResult();
}
这是我的HTML的一个片段,我有太多的控件,但它们几乎遵循相同的格式
<div class="form-group">
<label class="col-md-2 control-label" for="deviceName">Device Name: </label>
<div class="col-md-10">
<input id="deviceName" class="form-control" type="text" data-val="true" data-val-required="Device name is required" name="deviceName" value="TestName">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="deviceDisplay">Displayed As: </label>
<div class="col-md-10">
<input id="deviceDisplay" class="form-control" type="text" data-val="false" data-val-required="Device name is required" name="deviceDisplay" value="testDisplay">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="deviceDesc">Description: </label>
<div class="col-md-10">
<textarea id="deviceDesc" class="form-control" data-val="false" name="deviceDesc">Test desc</textarea>
</div>
</div>
设备名称:
显示为:
说明:
测试说明
如果我使用serializeArray(),它会返回31个条目,但是这些条目是
“dsCollection[0][name]”“dsCollection[0][value]”
索引一直到30序列化
方法在查询字符串中转换表单
字段,因此您只需将标记附加到该字符串,而不是创建另一个对象
如果您今天检查您的请求,您将看到您发布的值如下:
_RequestVerificationToken=token&dsCollection=deviceDesc%3Dcontent1%26deviceDisplay%3Dcontent2
当您有标准形式的反欺诈令牌时,令牌数据将与其他输入数据一起发送(因为实际上它只是一个隐藏的输入),因此正确的发送方式是:
deviceDesc=content1&deviceDisplay=content2&_RequestVerificationToken=token
另一件事是,看起来您的防伪令牌已经在您的表单中了
,所以您不需要做任何其他事情,只需表单。序列化
您可以在javascript代码中执行以下操作:
data: form.serialize()
你试过了吗:你有一行写着console.log(form);这个结果为空吗?@Luis.Andrade不,不是null@Johnathon64是的,有些事情正在发生,我不确定是什么,我不是专家,但我尽力帮助。我建议您使用这个助手,因为如果您有一个模型,它将为您编写ajax调用,我认为您可以将令牌作为参数发送。serialize是否还包括隐藏的输入字段?在我的收藏中,当我逐步浏览我的C#代码时,它们似乎没有被包括在内