You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在视图中显示模型项?遇RuntimeBinderException问题求助

问题解决方法

核心原因

你遇到的RuntimeBinderException是因为遍历ViewBag.DataList时,item被识别为JsonRoot类型,但该类型并没有Property属性——大概率是你在Controller中错误地将整个JsonRoot对象存入了ViewBag.DataList,而非JsonRoot内部的DataList集合;或者ViewBag的动态类型导致编译器无法正确推断item的实际类型(包含Property和oldValue的子模型)。

具体解决方案

方案1:修正ViewBag的赋值逻辑

检查Controller中的代码,确保存入ViewBag.DataList的是JsonRoot里的目标集合,而非JsonRoot本身:

// 错误示例:把整个JsonRoot对象存入ViewBag.DataList
// ViewBag.DataList = jsonRoot;

// 正确示例:存入JsonRoot内部的DataList集合
ViewBag.DataList = jsonRoot.DataList;

方案2:强类型转换遍历项

如果ViewBag赋值正确,但动态类型导致类型推断失败,遍历的时候强制将item转换为实际的子模型类型(替换成你定义的、包含Property的模型类名,比如DataListItem):

@foreach (var item in ViewBag.DataList as List<DataListItem>)
{
    <p>属性:@item.Property</p>
    <p>旧值:@item.oldValue</p>
}

方案3:改用强类型视图(推荐)

放弃使用ViewBag,直接使用强类型视图避免动态类型问题:

  1. 在视图顶部声明模型类型:
@model List<DataListItem>
  1. Controller中返回视图时传入目标集合:
return View(jsonRoot.DataList);
  1. 视图中直接遍历模型:
@foreach (var item in Model)
{
    <p>属性:@item.Property</p>
    <p>旧值:@item.oldValue</p>
}

内容的提问来源于stack exchange,提问作者jiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 20:45:34