gpt4 book ai didi

c# - MVC动态模型查看

转载 作者:行者123 更新时间:2023-11-30 18:28:43 25 4
gpt4 key购买 nike

the model item passed into the dictionary is of type 'System.Collections.Generic.Dictionary2[CapacityPlanner2.Models.Segment, CapacityPlanner2.Models.Product]', but this dictionary requires a model item of type 'System.Collections.Generic.Dictionary2[System.Object,System.Object]

我的 View 有@model Dictionary<dynamic, dynamic>

我正在传递以下 Dictionary<Project, Segment>

这不可行吗?

我的局部 View :

@model Dictionary<dynamic, Dictionary<dynamic, List<dynamic>>>

我的主视图

@model  Dictionary<dynamic, Dictionary<dynamic, Dictionary<dynamic, List<dynamic>>>>


@foreach (var kvp in Model)
{
@Html.Partial("_part", kvp.Value)
}

我想添加部分 View ,因为我想重用它。但在此之前一切正常。

最佳答案

如评论中所述,您必须在创建模型时传递 dynamic 类型。这是一个工作示例。但正如评论中提到的,请检查您是否真的需要dynamic,可能interfaced model 就足够了。

    public ActionResult Testing()
{
var data = new Dictionary<dynamic, Tuple<dynamic, dynamic>>
{
{1, new Tuple<dynamic, dynamic>(new Project{Name = "P1"}, new Segment{Id = "S1"})},
};

return View(data);
}

@foreach (var d in @Model)
{
var item1 = @d.Value.Item1 as Project;
var item2 = @d.Value.Item2 as Segment;
<p> @d.Key + ": " + @item1.Name + " - " + @item2.Id</p>
}

关于c# - MVC动态模型查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24329384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com