加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 部分视图模型无法解析

发布时间:2020-12-16 06:23:33 所属栏目:asp.Net 来源:网络整理
导读:我有一个模型A有一个属性是另一个模型类型,B.我有一个视图绑定到A.我想添加一个局部视图到A采取类型B的模型.这是我的代码 public class ModelA{ public bool Prop1 {get;set;} public bool Prop2 {get; set;} public Dictionaryint,string Prop3{get; set;}
我有一个模型A有一个属性是另一个模型类型,B.我有一个视图绑定到A.我想添加一个局部视图到A采取类型B的模型.这是我的代码

public class ModelA
{
    public bool Prop1 {get;set;}
    public bool Prop2 {get; set;}
    public Dictionary<int,string> Prop3{get; set;}
    public int Prop4 {get; set;}
    public ModelB Prop5 { get; set; }


    public ModelA ()
    {
        Prop5 = null;

       ... more code ...
    }
}

//This view is tied to ModelA
@using (Html.BeginForm("CreateReport","Home",FormMethod.Post))
{
   some markup
}

//this is the problem
@Html.Partial("FileLinks",Model.Prop5) //This line throws an error

Error: The model item passed into the dictionary is of type ‘ModelA’,but this dictionary requires a model item of type ‘ModelB’

如果我将其更改为@ Html.Partial(“FileLinks”,new ModelB()),该行可以工作

为什么原始代码不起作用?该属性为ModelB类型.

任何帮助表示赞赏谢谢!

更新:我忘了从Controller添加一些代码

m.FileLinks = new ModelB()
返回视图(“索引”,m)

所以模型不是空的

解决方法

我认为这里发生的是那个

当您渲染像这样的局部视图时,ViewDataDictionary和视图上下文将传递给局部视图因此,当ModelB为null时,则ViewDataDictionary< TModel>没有改变,并且在运行时,MVC引擎无法从空值确定模型的类型.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读