如何在View中获取ViewBag内存储的Dictionary数据
如何从存储Dictionary的ViewBag中获取数据?
我无法弄清如何从存储了Dictionary的ViewBag中获取数据,以下是后台相关代码:
if (dataSet.Tables[2].Rows.Count > 1) { evalMitModel = new EvaluationMitigationApproverModel(); evalMitModel.MitigationType = new List<string>(); evalMitModel.MitigationDescription = new List<string>(); evalMitModel.PersonResponsible = new List<string>(); evalMitModel.TargetDate = new List<string>(); string temp = dataSet.Tables[2].Rows[0]["id_hazard_trans"].ToString(); int counter = 1; for (int index = 0; index < dataSet.Tables[2].Rows.Count; index++) { if (temp == dataSet.Tables[2].Rows[index]["id_hazard_trans"].ToString()) { if (evalMitModel.HazardTransactionId == null) { evalMitModel.HazardTransactionId = dataSet.Tables[2].Rows[index]["id_hazard_trans"].ToString(); evalMitModel.HazardId = dataSet.Tables[2].Rows[index]["id_hazard"].ToString(); evalMitModel.ImpactId = dataSet.Tables[2].Rows[index]["impact_id"].ToString(); evalMitModel.LikeId = dataSet.Tables[2].Rows[index]["like_id"].ToString(); evalMitModel.Scale = dataSet.Tables[2].Rows[index]["Scale"].ToString(); evalMitModel.ApproverStatus = dataSet.Tables[2].Rows[index]["apprv_status"].ToString(); evalMitModel.ApproverComment = dataSet.Tables[2].Rows[index]["apprv_comment"].ToString(); } evalMitModel.MitigationType.Add(dataSet.Tables[2].Rows[index]["cnt_type"].ToString()); evalMitModel.MitigationDescription.Add(dataSet.Tables[2].Rows[index]["cnt_desc"].ToString()); evalMitModel.PersonResponsible.Add(dataSet.Tables[2].Rows[index]["pers_resp"].ToString()); } else { evalMitHashMap.Add(counter++, evalMitModel); evalMitModel = new EvaluationMitigationApproverModel(); evalMitModel.MitigationType = new List<string>(); evalMitModel.MitigationDescription = new List<string>(); evalMitModel.PersonResponsible = new List<string>(); temp = dataSet.Tables[2].Rows[index]["id_hazard_trans"].ToString(); index--; //evalMitModel.HazardTransactionId = null; } } evalMitHashMap.Add(counter, evalMitModel); ViewBag.EvaluationMitigationHashMap = evalMitHashMap; }
我尝试在视图中用Razor语法获取数据,但无法正确遍历:
@if (ViewBag.EvaluationMitigationHashMap != null) { foreach (var data in ViewBag.EvaluationMitigationHashMap) {
解决方法
ViewBag是动态类型,直接遍历会因类型不确定出现问题,需先强转为对应的Dictionary类型,再遍历键值对:
- 强转匹配类型:后台的
evalMitHashMap是Dictionary<int, EvaluationMitigationApproverModel>类型(键为int类型的counter,值为自定义模型),视图中先将ViewBag内容强转为此类型。 - 遍历键值对:每个遍历项是
KeyValuePair<int, EvaluationMitigationApproverModel>,通过Key获取序号,通过Value访问模型的所有属性,List类型属性需嵌套遍历。
修正后的视图代码示例:
@if (ViewBag.EvaluationMitigationHashMap != null) { // 强转为对应的Dictionary类型 var mitigationDataMap = ViewBag.EvaluationMitigationHashMap as Dictionary<int, EvaluationMitigationApproverModel>; if (mitigationDataMap != null) { foreach (var kvp in mitigationDataMap) { // 访问后台的counter序号 <div>序号: @kvp.Key</div> // 访问模型基础属性 <div>危险事务ID: @kvp.Value.HazardTransactionId</div> <div>审批状态: @kvp.Value.ApproverStatus</div> <div>审批意见: @kvp.Value.ApproverComment</div> // 遍历List类型的缓解措施类型 <div> <h5>缓解措施类型:</h5> @foreach (var type in kvp.Value.MitigationType) { <span>@type </span> } </div> // 遍历List类型的缓解措施描述 <div> <h5>缓解措施描述:</h5> @foreach (var desc in kvp.Value.MitigationDescription) { <p>@desc</p> } </div> } } }
额外提示
- 建议在后台显式声明
evalMitHashMap的类型,避免隐式类型带来的不确定性:var evalMitHashMap = new Dictionary<int, EvaluationMitigationApproverModel>(); - 强转时必须保证类型完全匹配,否则会返回null,因此要添加null判断。
内容的提问来源于stack exchange,提问作者Musaib Bilal
相关产品推荐
相关产品推荐

