隐藏的输入字段未被序列化

时间:2015-06-30 18:43:20

标签: javascript jquery html html5 asp.net-mvc-5

我在表单上有一些隐藏字段,我稍后会使用javascript为其分配值。但是当我将表单发布到我的控制器上时,它接受一个FormCollection作为参数,它似乎没有拾取隐藏的字段,只看到可见的输入字段。

 <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>

这是我的ajax post方法

$(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;
    })
});

})

0 个答案:

没有答案