Javascript 隐藏的输入字段未序列化
我在表单上有一些隐藏字段,稍后使用javascript为其赋值。但是,当我将表单发布到接受FormCollection作为参数的控制器时,它似乎不会拾取隐藏字段,只拾取可见的输入字段Javascript 隐藏的输入字段未序列化,javascript,jquery,html,asp.net-mvc-5,Javascript,Jquery,Html,Asp.net Mvc 5,我在表单上有一些隐藏字段,稍后使用javascript为其赋值。但是,当我将表单发布到接受FormCollection作为参数的控制器时,它似乎不会拾取隐藏字段,只拾取可见的输入字段 <div id="deviceInfoPanel" class="panel-body"> @using (Html.BeginForm("Settings", "EditDatasource", FormMethod.Post, new { id = "__dsAjaxAnt
<div id="deviceInfoPanel" class="panel-body">
@using (Html.BeginForm("Settings", "EditDatasource", FormMethod.Post, new { id = "__dsAjaxAntiForgeryForm", @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div id="deviceinfo">
<!--This is where the device info content will be created
This will be created dynmically by making an AJAX call
to the server bringing back the information required
to construct the HTML dynamically via javascript.-->
@Html.Hidden("dsID");
@Html.Hidden("dsName");
@Html.Hidden("dsDesc");
@Html.Hidden("dsOID");
@Html.Hidden("dsMetric");
@Html.Hidden("dsValueMin");
@Html.Hidden("dsValueMax");
@Html.Hidden("dsAlertThreshold");
@Html.Hidden("dsNoData");
@Html.Hidden("dsAlertTrigInt");
@Html.Hidden("dsAlertClearInt");
@Html.Hidden("dsAlertSubject");
@Html.Hidden("dsAlertBody");
@Html.Hidden("dsDeletedDP");
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Apply" id="postEditDatasource" class="btn btn-default" />
</div>
</div>
}
</div>
})
serializeArray
方法将隐藏字段序列化为任何其他字段。恐怕问题还在于其他方面。您是否可以使用浏览器的开发工具,特别是网络
选项卡,查看将哪些数据发送到服务器以供发布
?这应该会显示从表单中获取的所有数据,并且您可以准确地看到隐藏字段是否正在传递到服务器。我正好遇到了这个问题,没有隐藏类型的问题,但它不会工作。serializeArray
方法将隐藏字段序列化为任何其他字段。恐怕问题还在于其他方面。您是否可以使用浏览器的开发工具,特别是网络
选项卡,查看将哪些数据发送到服务器以供发布
?这应该会显示从表单中获取的所有数据,并且您可以准确地看到隐藏字段是否正在传递到服务器。我确实有这个问题,没有隐藏类型的问题,但是使用它,它将不起作用。
$(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.
//__RequestVerificationToken: token,
$('#__dsAjaxAntiForgeryForm').on('submit', function () {
$.ajax({
url: URL,
data: form.serializeArray(),
type: 'POST',
success: function (result) {
alert('this worked')
if (result.result == "Error") {
ShowDatasourcePostAlert('failPost', 3000);
} else {
ShowDatasourcePostAlert('successPost', 3000);
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR + ', ' + textStatus + ', ' + errorThrown);
}
})
return false;
})
});