I know that view model can be used for rendering a view, but if a page needs different models, how can I pass them to the view? And how do I use them?
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- MVC-Routing,Why i can not ignore defaults,The matc
- How to store image outside of the website's ro
- 'System.Threading.ThreadAbortException' in
- Request.PathInfo issues and XSS attacks
相关文章
- asp.net HiddenField控件扩展问题
- asp.net HiddenField控件扩展问题
- Asp.Net网站无法写入错误日志,测试站点可以,正是站点不行
- asp.net mvc 重定向到vue hash字符串丢失
- FormsAuthenticationTicket expires too soon
- “Dynamic operations can only be performed in homog
- What is the best way to create a lock from a web a
- Add to htmlAttributes for custom ActionLink helper
If you need to pass multiple models then create an all-encompassing model that has the smaller models hanging off as properties.
For instance, let's say you are going to display a page for managing groups of users for your app. You would probably need to pass an
IEnumerable<UserDisplayModel>
and also anIEnumerable<GroupDisplayModel>
to the view. Create a new display model like this:Pass instances of this model to your view instead.
If you find that you want to do this a lot and that you don't much care for creating lots of small types, then you can use .NET 4's
dynamic
type:In your action, pass an anonymous type to your view:
The reference them in your view as you would otherwise.
You lose intellisense and strong typing this way, but unless you're compiling your views (which requires manually editing your .csproj file and makes the build much slower) then you don't have compile time checking on your views anyway.