【问题标题】:@Html.HiddenFor() in mvc 3 doesn't pass data to controllermvc 3 中的 @Html.HiddenFor() 不会将数据传递给控制器
【发布时间】:2013-08-05 13:00:44
【问题描述】:

我有一个名为“AirLineSession”的类:

    public ExtendedList listCo { get; set; }
    public ExtendedList listBa { get; set; }

    public bool SortbyRating { get; set; }
    public AirLineSession()
    {
        listCo = new ExtendedList();
        listBa = new ExtendedList();
    }

在我看来,我有:

@Html.HiddenFor(m => m.listBa)
@Html.HiddenFor(m => m.listCo)
@Html.DropDownListFor(a => a.SortbyRating, new[] { new SelectListItem { Text = "Sort by rating", Value = "true" }, new SelectListItem { Text = "Sort by frice", Value = "false" } })

当我登上这个视图时,只有 SortByRating 的数据被传递给 Controller 中的模型,listBa 和 listCo 没有被传递。我不知道为什么。
谁能帮帮我,谢谢。

【问题讨论】:

  • 可以发一下ExtendedList类的代码吗?原因是asp.net mvc 不知道如何为ExtendedList 复杂类型创建所有隐藏。在这种情况下,您应该为对象和集合的每个字段添加隐藏。
  • 您是否尝试过使用 FormCollection 作为第二个参数并尝试检查该参数的键和值???
  • Html.HiddenFor 默认只能“存储”基本类型(即字符串、整数、小数、枚举等)。它不能包含复杂的类,从外观上看,您拥有的 ExtendedList 是一个类。

标签: c# asp.net-mvc asp.net-mvc-3 html.hiddenfor


【解决方案1】:

你能发布扩展列表类的代码吗?原因是 asp.net mvc 不知道如何为 ExtendedList 复杂类型创建所有 Hidden。在这种情况下,您应该为两个集合的对象和集合的每个字段添加隐藏,例如:

@for(var i = 0; i < Model.listCo.Lenght; i++)
{
    @Html.HiddenFor(model => model.listCo[i].Property1)
    @Html.HiddenFor(model => model.listCo[i].Property2)
    @Html.HiddenFor(model => model.listCo[i].Property3)
}

还有:

@for(var i = 0; i < Model.listBa.Lenght; i++)
{
    @Html.HiddenFor(model => model.listBa[i].Property1)
    @Html.HiddenFor(model => model.listBa[i].Property2)
    @Html.HiddenFor(model => model.listBa[i].Property3)
}

【讨论】:

    【解决方案2】:

    如果您使用 For end html 帮助程序,请确保您已将它们的值按模型传递给相关视图。但如果没有,您应该将值赋给一些动态工具,例如 ViewBag,然后使用以下代码:

    @Html.Hidden("listBa", ViewBag.listba)
    @Html.Hidden("listCo", ViewBag.listco)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多