开发者

How to have a strongly typed partial view?

开发者 https://www.devze.com 2022-12-15 06:34 出处:网络
Say I have just a normal view(not strongly typed) and in this view I have this <% Html.RenderPartial(\"ViewUserControl1\") %>

Say I have just a normal view(not strongly typed) and in this view I have this

 <% Html.RenderPartial("ViewUserControl1") %>

However this Partial View is strongly typed with a class(lets call it class1).

How do I pass data to this strongly typed view? Like in my partial view I can't go right now and do something like this

It would just crash. So I need to pass class1 som开发者_如何学Goehow to this strongly typed view and preferably keep the view non typed.


 <% Html.RenderPartial("ViewUserControl1", class1object) %>

or if you are using ViewData["something"] in non typed view to hold that class1 you can cast it to class1

<% Html.RenderPartial("ViewUserControl1",(class1)ViewData["something"] %>

just like that


If you can't make your parent view strongly typed, you could just add the model for your partial view to the viewdata dictionary, and pass it to the partial view, like so:

<% Html.RenderPartial("ViewUserControl1", ViewData["MyDataObject"] %>
0

精彩评论

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