开发者

pass an object to a user controls ViewModel in asp.net mvc

I h开发者_StackOverflow中文版ave a View that uses a specific ViewModel.

The viewModel has various object e.g. Foo, Bar...etc

I have a user control that has its own ViewModel which contains a Foo object.

How do pass the Foo object from the page View to the usercontrols ViewModel?


If you do this:

<% Html.RenderPartial("partial", Model.Foo); %>

Then one of two things will happen.

  1. If the View's Model.Foo is non-null, then the UserControl's Model will be equal to the View's Model.Foo, and the UserControl's Model.Foo will be the View's Model.Foo.Foo.
  2. *If the View's Model.Foo is null, then the UserControl's Model will be equal to the View's Model, and the UserControl's Model.Foo will be the View's Model.Foo. If the View's Model and Model.Foo are not the same type and the View's Model is non-null and if the UserControl uses strongly typed view data, then you will get a runtime error since the UserControl's model is now of type TViewModel instead of TUserControlModel.


Are you looking for this:

<% Html.RenderPartial("partial", Model.Foo); %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜