Joh*_*n64 4 html javascript c# jquery asp.net-mvc-5
我正在尝试将表单发布到控制器,其中控制器接受FormCollection作为参数。当我尝试访问dsCollection中的项目时,我得到了null。FormCollection中的唯一项目是"__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;
})
});
})
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
[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();
}
Run Code Online (Sandbox Code Playgroud)
这是我的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>
Run Code Online (Sandbox Code Playgroud)
如果我使用serializeArray(),它会给我返回31个条目,但是这些条目"dsCollection[0][name]" "dsCollection[0][value]"的索引一直上升到30
serialize方法将form查询字段转换为查询字符串,因此您应该将令牌附加到该字符串,而不是创建另一个对象。
如果今天检查您的请求,您会看到您正在发布一个类似这样的值:
_RequestVerificationToken=token&dsCollection=deviceDesc%3Dcontent1%26deviceDisplay%3Dcontent2
Run Code Online (Sandbox Code Playgroud)
当您具有标准形式的防伪令牌时,令牌数据将与其他输入数据一起发送(因为实际上这只是一个隐藏的输入),因此发送它的正确方法是:
deviceDesc=content1&deviceDisplay=content2&_RequestVerificationToken=token
Run Code Online (Sandbox Code Playgroud)
另一件事是,您的防伪令牌似乎已经在您的内部,form因此您不需要做其他任何事情,而不仅仅是form.serialize。
您可以在JavaScript代码中执行以下操作:
data: form.serialize()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1761 次 |
| 最近记录: |