【问题标题】:Form deserialization not working表单反序列化不起作用
【发布时间】:2012-10-15 16:02:13
【问题描述】:

我有一个异步 httpPost 序列化我的表单数据并将其提交给我的控制器。在那里我尝试将我的表单数据序列化到我的视图模型类中,但所有值都为空或分配了默认值。

public ActionResult GetSalesData(string vmString)
{
    -serialization...
    -use the data to select some other data
    return new JsonResult { Data = d, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

对于序列化我到目前为止尝试了这两种方式:

1.)

    { vmString: $('form').serialize() }

结果:

Dataset.Ids=3,7,12&Type=Sales&DataSources=BeverageType&DisplayModes=Volume&SeriesTypes=Lines&SalesVm.DisplayOptions.ShowAverage=false&SalesVm.DisplayOptions.ShowTargetLine=false&ActionName=Sales_SelectBeverageTypes&Mode=LightboxInWizard&SelectedDatasetIdsWorkingCopy=3&SearchTerm=

2.)

    { vmString: JSON.stringify($('form')) }

结果:

{"length":2,"prevObject":{"0":{"jQuery1710039964356962994385":1,"location":{}},"context":{"jQuery1710039964356962994385":1,"location":{}},"length":1},"context":{"jQuery1710039964356962994385":1,"location":{}},"selector":"form","0":{"Dataset.Ids":{},"Type":{},"DataSources":{"jQuery1710039964356962994385":15},"3":{"jQuery1710039964356962994385":16},"DisplayModes":{"0":{"jQuery1710039964356962994385":42},"1":{"jQuery1710039964356962994385":43},"2":{"jQuery1710039964356962994385":44},"3":{"jQuery1710039964356962994385":45}},"SeriesTypes":{"0":{"jQuery1710039964356962994385":46},"1":{"jQuery1710039964356962994385":47}},"SalesVm.DisplayOptions.ShowAverage":{"0":{"jQuery1710039964356962994385":48},"1":{"jQuery1710039964356962994385":49}},"SalesVm.DisplayOptions.ShowTargetLine":{"0":{"jQuery1710039964356962994385":50},"1":{"jQuery1710039964356962994385":51}}},"1":{"ActionName":{},"Mode":{},"SelectedDatasetIdsWorkingCopy":{},"SearchTerm":{"jQuery1710039964356962994385":35}}}

为了反序列化,我试过了:

m = (StatisticsViewerViewModel)new JsonSerializer().Deserialize(new System.IO.StringReader(vmString), typeof(StatisticsViewerViewModel));
m = (StatisticsViewerViewModel)new JsonSerializer().Deserialize(new System.IO.StringReader(vmString), vmString.GetType());
m = JsonConvert.DeserializeObject<StatisticsViewerViewModel>(vmString);

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(StatisticsViewerViewModel)); 
m = (StatisticsViewerViewModel) ser.ReadObject(stream);

我检查了我的所有字段都在表单内:

 @using (Html.BeginForm())
{
   some partial views containing some fields, all using the same model..
}

我还检查了使用的视图模型以及所有嵌套模型确实有一个空的构造函数。

是否有不同的方法来反序列化表单数据?还有什么我可以检查以确保一切正常?

  • 编辑-

ViewModel-根:

public class StatisticsViewerViewModel
{
    public String BreadcrumbName { get; set; }
    public String ActionName { get; set; }
    public StatisticsType Type { get; set; }
    public DropDownListModel DataSourceList { get; set; }
    public MultiSelectionModel Dataset { get; set; }
    public SalesViewModel SalesVm { get; set; }
    //public EventsViewModel EventsVm { get; set; }
    public ChartExportOptions ExportOptions { get; set; }

    public StatisticsViewerViewModel()
    {
        DataSourceList = new DropDownListModel();
        Dataset = new MultiSelectionModel();
        SalesVm = new SalesViewModel();
        ExportOptions = new ChartExportOptions();
    }

    public enum StatisticsType
    {
        Sales,
        Events
    }
}

嵌套视图模型

public class SalesViewModel
{
    public SalesDisplayOptions DisplayOptions { get; set; }
    public RadioButtonModel DisplayModes { get; set; }      // Volume, Value, ..
    public RadioButtonModel SeriesTypes { get; set; }       // Line, Bar, ..

    public SalesViewModel(bool initialize = false)
    {
        if (initialize) { Initialize(); }
    }
}

public class SalesDisplayOptions
{
    public DisplayMode Mode { get; set; }
    public SeriesType Type { get; set; }
    public bool ShowAverage { get; set; }
    public bool ShowTargetLine { get; set; }

    public enum SeriesType
    {
        Lines, ...
    }

    public enum DisplayMode
    {
        Value, ...
    }

}

相关的一点可能是帖子被 kendoChart 解雇了。这是视图中有趣的块:

....
<div id="chart"></div>
....
@this.ScriptBlock(
@<script type="text/javascript">
$("#chart").kendoChart({
             dataSource: new kendo.data.DataSource({
                 transport: {
                     read: {
                         url: actionUrl,
                         data: { vmString: $('form').serialize() },
                         dataType: "json",
                         contentType: "application/json; charset=utf-8"
                     }
                 },
                 sort: ...
 ....

为了检查问题是否与 kendoChart-Object 相关,我测试性地实现了手动 POST:

....
<div class="round-corner-bottom-right">
        <button id="send-form" type="button">
            <span class="button-label">Send Form Data</span>
        </button>
</div>
....

@this.ScriptBlock(
@<script type="text/javascript">

     $('document').ready(function () {

        $('#send-form').on('click', function () {

             var data = $('form').serialize();

             $.ajax({
                 type: "POST",
                 url: actionUrl,
                 data: data,
                 cache: false,
                 success: function (returnData) {
                 }
             });

     }); // document ready

在这种情况下问题不会持续存在,所有值都按预期设置。所以 kendoChart 方法似乎有问题。

【问题讨论】:

  • 您能发布您的 Viewmodel C# 和 .aspx 标记吗?您所追求的功能内置于 MVC 模型绑定器中。如果我们可以让您的代码使用它而不是滚动您自己的代码,它将为您省去很多麻烦。
  • @David 您需要发布您的虚拟机并查看代码。我一直这样做,而且大多数时候都没有问题,而且我在 Vm 或数据集合中有 Vms 并且它绑定了。你可以试试 serializeArray();你也在使用 html 助手吗?
  • 好的!这是我的虚拟机。希望对你有帮助

标签: c# asp.net asp.net-mvc json http-post


【解决方案1】:

您是否厌倦了使用允许默认模型绑定器来处理它而不是尝试自己反序列化和管理它?

所以

public ActionResult GetSalesData(string vmString)

变成这样

public ActionResult GetSalesData(MyModel vmString)

【讨论】:

  • 是的,我也试过了,抱歉我忘了提。那么整个对象为空。
  • 你是否将模型的PK作为隐藏字段包含在内?
  • 我没有任何 PK,因为我的视图模型没有直接附加到数据库。我要发布我的虚拟机..
  • 好的,我们可以看看你的视图模型定义和你的视图吗?
  • 我在原帖中添加了虚拟机
【解决方案2】:

根据我的经验,将复杂的 JS 对象传递给 Asp.net 控制器动作并将数据序列化为视图模型的唯一简单方法是首先在 JS 端预先格式化对象。

本页说明方法:http://erraticdev.blogspot.com/2010/12/sending-complex-json-objects-to-aspnet.html

要预先格式化数据,您可以这样做:

$.post(URL, $.toDictionary({ ... }), function(response) { ... });

这是 jQuery.toDictionary 插件的缩小源代码:

/*!
* jQuery toDictionary() plugin
*
* Version 1.2 (11 Apr 2011)
*
* Copyright (c) 2011 Robert Koritnik
* Licensed under the terms of the MIT license
* http://www.opensource.org/licenses/mit-license.php
*/
(function (a) { if (a.isFunction(String.prototype.format) === false) { String.prototype.format = function () { var a = this; var b = arguments.length; while (b--) { a = a.replace(new RegExp("\\{" + b + "\\}", "gim"), arguments[b]) } return a } } if (a.isFunction(Date.prototype.toISOString) === false) { Date.prototype.toISOString = function () { var a = function (a, b) { a = a.toString(); for (var c = a.length; c < b; c++) { a = "0" + a } return a }; var b = this; return "{0}-{1}-{2}T{3}:{4}:{5}.{6}Z".format(b.getUTCFullYear(), a(b.getUTCMonth() + 1, 2), a(b.getUTCDate(), 2), a(b.getUTCHours(), 2), a(b.getUTCMinutes(), 2), a(b.getUTCSeconds(), 2), a(b.getUTCMilliseconds(), 3)) } } var b = function (c, d, e, f) { if (a.isPlainObject(c)) { for (var g in c) { if (f === true || typeof c[g] !== "undefined" && c[g] !== null) { b(c[g], d, e.length > 0 ? e + "." + g : g, f) } } } else { if (a.isArray(c)) { a.each(c, function (a, c) { b(c, d, "{0}[{1}]".format(e, a)) }); return } if (!a.isFunction(c)) { if (c instanceof Date) { d.push({ name: e, value: c.toISOString() }) } else { var h = typeof c; switch (h) { case "boolean": case "number": h = c; break; case "object": if (f !== true) { return }; default: h = c || "" } d.push({ name: e, value: h }) } } } }; a.extend({ toDictionary: function (c, d, e) { c = a.isFunction(c) ? c.call() : c; if (arguments.length === 2 && typeof d === "boolean") { e = d; d = "" } e = typeof e === "boolean" ? e : false; var f = []; b(c, f, d || "", e); return f } }) })(jQuery)

【讨论】:

    【解决方案3】:

    问题不在于反序列化过程本身,而在于数据在 kendoChart 数据源中的绑定方式。该问题的解决方案是使用 ajax post 指定一个函数作为数据源,如下所示:

    var data = $('form').serialize();
    
    $("#chart").kendoChart({
                 title: {
                     text: "Solution"
                 },
                 dataSource: new kendo.data.DataSource({
                     transport: {
                         read: function (options){
                             $.ajax({
                                 url: actionUrl,
                                 data: data,
                                 type: "POST",
                                 cache: false,
                                 success: function (result) {
                                     options.success(result);
                                 }
                             });
                         }
                     },
                     ....
    

    很抱歉用错误的问题打扰您!

    【讨论】:

      猜你喜欢
      • 2013-03-21
      • 2012-07-11
      • 2020-10-16
      • 2012-09-18
      • 2016-01-26
      • 2015-04-17
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多