繁体   English   中英

Form.serialize()似乎返回null

[英]Form.serialize() seems to return null

我试图将一个表单发布到控制器,其中控制器接受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;
    })
});
})

这是我的控制器:

    [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

serialize方法在查询字符串中转换form字段,因此您只需将标记附加到该字符串而不是创建另一个对象。

如果您今天检查了您的请求,您会看到您发布的值如下:

_RequestVerificationToken=token&dsCollection=deviceDesc%3Dcontent1%26deviceDisplay%3Dcontent2

当你有一个标准形式的反伪造令牌时,令牌数据与其他输入数据一起发送(因为实际上它只是一个隐藏的输入)所以发送它的正确方法是:

deviceDesc=content1&deviceDisplay=content2&_RequestVerificationToken=token

另外一点是,看起来您的防伪标记已经在您的form因此除了form.serialize之外您不需要做任何其他事情。

您可以在javascript代码中执行以下操作:

data: form.serialize()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM