How to pass model to partial view

后端 未结 3 1157
悲哀的现实
悲哀的现实 2020-12-16 09:06

I have two view models:

public class ParentViewModel
    {
        public Id { get; set; }
        .....
        public ChildViewModel Child{ get; set; }
            


        
3条回答
  •  -上瘾入骨i
    2020-12-16 10:09

    I had the same issue as the OP. From one of the comments, I realized that the second parameter shouldn't be null, i.e from

    @model ParentViewModel
    @Html.Partial("_Partial", Model.Child)
    

    If Model.Child is null, then Model is passed instead of Model.Child. If there will be cases when the second parameter is null, then you will have to check first in your code and maybe pass an initialized Child as the second parameter. Something like this

    @Html.Partial("_Partial", new Child())
    

提交回复
热议问题