Get Model Type from RazorView object - asp.net-mvc

I have a System.Web.Mvc.RazorView object which is strongly typed when in cshtml.
Can I get the model type from an instance of this class?

This is possible.
Call BuildManager.GetCompiledType(view.ViewPath) to get the type generated by compiling the view.
You can find the model type by checking the generic argument of the compiled type's base type (which should be WebViewPage<TModel>)

There's no way to get the model given only an instance of a System.Web.Mvc.RazorView. It's available inside the RenderView method which is passed a ViewContext but from the outside you can't access it. But if you are inside a view you could use the Model property.

Related

Instantiating a class by String name in Dart

I am trying to call a method of a class that I only know by name as a String. Now therefore I would need a ClassMirror of that class that allowes me to instantiate an instance. However, creating ClassMirrors seems to be only possible by entering a type using reflectClass(Type) or by passing an already existing instance of that class into reflect(dynamic). So these aren`t helping if I only have a String.
In Java you can do this pretty easily, by calling Class.forName(String). Then you would get a Constructor instance, make it accessibly and call it.
Does anyone know if this is even possible in dart? What seems weird is that once you have a ClassMirror you can access fields and methods by passing symbols, which can be created by Strings.
You can put a specific list of strings to map to a specific list of closures to create a new object with specific parameters.
But you can't get a reflection without using dart:mirrors, which is being deprecated, and also had a negative impact on tree shaking to get the payload size down.
In general, you're invited to look at the package:reflectable to achieve most of what you'd want out of dart:mirrors, using source-to-source builders.

F# not recognizing members of a record type

I am doing a simple mapping and the compiler doesn't recognize the record's members.
The image shows the type OpeningHours itself is clearly ok, with all 3 properties showing:
But hovering over the marked property shows:
error FS0039: The field, constructor or member 'Day' is not defined.
Namespaces are all referenced and I even assign to the very same properties few lines below without any issue.
OpeningHours is a class, not a record. This is one way to create an instance of the class:
OpeningHours(day = oh.Day, opens = oh.Opens, closes = oh.Closes)
Based on the constructor and properties, it looks like OpeningHours is defined as a class, not a record. The record syntax can't be used with classes, so the solution is either to change OpeningHours to a record, or to instantiate it using its existing constructor.

Pass anonymous type as model in MVC 3 Partial View

I am refactoring an MVC 3 application, and moved a set of similar items into a partial view so I can keep that template DRY. Since the pieces don't all have the exact same properties, I am creating anonymous types like this:
var model1 = new { Description = "description 1", Message = "message 1" }
and passing them to the partial view like so:
#Html.Partial("_Partial", model1)
The partial view is then attempting to render certain blocks based on existence of a specific property, i.e.
#if (Model.Description != null)
{
#Model.Description
}
My issue is that even though I can see and navigate the Model object in the watch window during execution, I get a RuntimeBinderException in the if test that states 'object' does not contain a definition for 'ShowApplied'. I can obtain the values through reflection by calling (Model.GetType().GetProperty("ShowApplied").GetValue(Model)), but would much rather use the format shown in my code sample. I have been unable to find a clean solution...
How can I pass an anonymously-typed object to a partial view and access its properties directly? I feel like there is something simple I'm missing...
Why am I able to see the Model properties while debugging, but not access them from code?
EDIT
I am specifying #model dynamic.
Using an interface requires creating non-anonymous types because, as this answer explains,
An anonymous type cannot be cast to any interface or type except for object.
Insights from the comments (thank you) imply I have 2 options, since (as the answer to the linked question points out),
Anonymous types are internal, so their properties can't be seen outside their defining assembly.
and therefore are inaccessible to the Razor binding engine.
Use #Html.DisplayFor("amount") and deal with not having IntelliSense, reference lookups, etc.
Create classes that implement a common interface and bind my partial view to that interface.

How to cast IQueryable to IQueryable<T> when I don't know the T in advance?

I have an asp.net mvc view, which is using Telerik's grid.
Html.Telerik.Grid(Model.Items)
Model.Items is IQueryable, but the Grid requires me to cast it:
For example:
Html.Telerik.Grid((IQueryable<Product>)Model.Items)
The problem is: I don't know what type is in Model.Items (it can be IQueryable<Product>, IQueryable<Book> and many others).
What I also have is Model.ItemsType, which can have following values: typeof(IQueryable<Product>), typeof(IQueryable<Book>)...
How can I cast the IQueryable to IQueryable<T>, when I don't know the type of T in advance?
Generic types need to be known at compile-time, not determined at run-time.
What is the type of Model.Items? If it's derived from IEnumerable<T> then assuming you have a reference to System.Linq then you should be able to call the AsQueryable() extension method.
You're going to need to use reflection to invoke the Grid method when you don't know the T ahead of time, because there's no way to do it at compile time. If Model.Items actually is an IQueryable, then you don't need to cast it; just pass it along as the parameter during the reflection-based invocation (which takes all parameters as type object anyway).
Perhaps you can try binding to IEnumerable. Check the 'Binding to a collection of dynamic objects with MVC3 Razor' code library entry.

ASP.NET MVC: How do I access my strongly-typed model from within a HtmlHelper extension method?

I can get at the ViewData and the ViewContext but not the Model.
Any ideas? Do I need to pass the model into the extension method as a parameter? Doesn't seem ideal.
Found it!
helper.ViewData.Model
If you are depending on the type in the helper extension, I would prefer to have it passed as a parameter. This way you know that it has the proper type when you construct the method (won't compile if the model is not of the proper type). If you access it in the helper as a property of the ViewData, you won't know until runtime if it has the correct type and will be forced to throw an exception -- or handle it as an error. Neither of which are particularly good options, in my opinion.

Resources