开发者

ASP.NET MVC: Return different views depending on object type

开发者 https://www.devze.com 2023-02-02 12:00 出处:网络
In my controller, I\'m generically deserializing Xml. The object being deserialized to could end up being any number of different types. This, of course, won\'t be known at compile time. I\'ve created

In my controller, I'm generically deserializing Xml. The object being deserialized to could end up being any number of different types. This, of course, won't be known at compile time. I've created a number of partial views, each of which are strongly typed to a different object that is a possible outcome of the Xml deserializaton.

My problem is that I'm struggling with a good way to return the right view. I could always switch on some property in the object, or use a Key/ViewName Dictionary mapping to get the right view name, but I was hoping for something a little more generic than that.

Does anyone know of a way that I could implicitly say, "return the view that is typed off of the object I have."开发者_运维问答?

Thanks.


Without seeing your code it's hard to tell if what you're doing fits in with any best practice however from the outside, it does seem like your approach might be somewhat confusing and restrictive.

That said, you could simply name your views using the type name.

something like return View(xmlObject.GetType().Name + "View");

0

精彩评论

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