MVC :: Runtime Binder Exception When Using Anonymous Type In Untyped View?
Aug 14, 2010
I'm using MVC, and in the controller, I'm using a linq to objects query which returns an anonymous type:
var results = from ... select new { ... };
I then pass this information to an untyped view:
return View(results);
I try to iterate through the data in the view:
<% foreach (var entry in Model)%>
...
<%: Html.Encode(entry.MyProperty) %>
However, I get a RuntimeBinderException: 'object' does not contain a definition for 'MyProperty'. However, oddly, if I hover above entry with the mouse, it does actually have MyProperty in the popup window, and the value is what I would expect,(apparently, the Visual Studio IDE knows what type it is).I can get around this problem by using a linq query which uses an explicit type and a parameterless constructor of the form:
IQueryable<SearchResult> results = from ... select new SearchResult { ... };
and creating a view model class which encapsulates the SearchResult data, which is then returned to a strongly typed view. However, I don't understand what the problem is with the first method.
I have controller method that looks something like this:
[Code]....
notice the commented out line. This method used to return an IEnumerable<Sport> but now that I have used a LINQ query it is returning an IEnumerable of an anonymous type (I think that's the correct terminology - please correct me if I'm wrong).
Question I have is...can I add a strongly-typed view based on this anonymous type and if not, how do I write a view that can access this collection?
I then retrieve the data from the DataTable into an anonymous type:
var feeds = from feed in categoryProducts.AsEnumerable()[code]....
This all works great.
However, I would like to extend the code to perform some evaluation checks (e.g., check that various columns in the DataTable are not NULL) and other pre-processing (e.g., call various functions to build the image URL based on the image ID - which is another column in the DataTable not shown in the code fragment) before I return the resulting rows of the DataTable as an anonymous type to the client-side.Basically, I want to iterate through the DataTable, perform the evaluation checks and pre-processing, while building my anonymous type manually as I go. Or maybe there is a better way to achieve this?
I am trying to figure out how to get at an anonymous typed object's properties, when that anonymous type isn't created in the current function.
Specifically, I am binding an ASP.NET ListView to LINQ resultset, then trying to process each item in the ItemDataBound event.
Option Explicit On Option Strict On Class MyPageClass Private Sub Bind() Dim items As ItemData = FetchItemData() Dim groups = From s In items Group s By Key = s.GroupId Into Group _ Select GroupID = Key, GroupData = Group ' This works fine: Dim groupId As Integer = groups(0).GroupID lvGroups.DataSource = groups lvGroups.DataBind() End Sub Private Sub lvGroups_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs) Handles lvGroups.ItemDataBound If e.Item.ItemType = ListViewItemType.DataItem Then Dim item As ListViewDataItem = DirectCast(e.Item, ListViewDataItem) Dim groupData = item.DataItem ' This is the anonymous type {GroupId, GroupData} ' Next Line Doesn't Work ' Error: Option Strict disallows late binding Dim groupId As Integer = groupData.GroupId End If End Sub End Class
What do I need to do in lvGroups_ItemDataBound() to get at the item.DataItem.GroupId?
Basically, I have a repeater control, and a Linq query that retrieves some items. Ordinarily I would databind straight to the query and use Eval to populate the template with the results.
However, it doesn't come through in quite the right format - for example, if EndDate is null (it's a DateTime?) then I want to substitute it with "Present". I am using only a couple of the properties in the query result objects.
I use LinqToSql in my project. I have a class called MessageBLL and this class has a method called GetAll which returns List<object>. I returned object because I get data from two tables. Here is my code;
[Code]....
I want to use this in my Default.aspx.cs as like this but intellisense is not show CategoryName :[Code]....suggest me return as IQueryable but I couldn't do it in that way.
Do any one know how to return an anonymous type. I am using Linq where i need to return the following code
private <What's the return type to be provided here> SampleLinq(Int32 Num) { var query = (from dept in obj.DeptTable where dept.Id == Num select new { dept.DeptName, dept.DeptId }); return (query) }
I am consuming an RSS feed to display on my website using a repeater control. I was wondering if it's possible in VB to return an anonymous type from my linq query rather than a collection of strongly typed RSSItems. I know this is possible in C#, however haven't been able to work out a VB equivalent.
It seems my Eval(Container.DataItem, "JobNID") is not working and I assume this is because the datasource of the outer (parent) repeater is an anonymous type. So how am I supposed to access a particular field of my anonymous type?
I have simple 3 tier web application and have mostly CRUDE functionalities. Recently I required to add new console application to the existing solution in which I call data layer methods for retrieving data from DB but I get an exception "The type initializer for threw an exception."When I debugged I found that the exception is thrown at datalayer on first line of class where I get connectionstring from
web.config, the code is public static readonly string CONNECT_STRING =
Now if I hardcode the connection string value like public static readonly string CONNECT_STRING = "Data Source=XYZ;uid=sa;password=XXX;initial catalog=ABC;"
it works fine.I don't understand what is the issue here as web application works fine with this datalayer.
I have a web service and its methods return a class which is name WSResult. WSResult has 2 properties. One of property's type is int and the other one's type is object. I want to return some different type with this second property.
[code]....
How can i pass an object which i retrieved from other web services or that i generated from my serializable classes inside M_ResultObject property ?
We have an asp.net 2.0 application in which we get the exception of type 'System.OutOfMemoryException' whenever value is retrieved or stored in viewstate. It does not happen when we test it in local PC but when it is deployed on application server, the exception is thrown.
I have one issue , on that page i have Gridview ie: binding data(by making relationship b/w two tables ) ie: each row contains 75 EasyListbox control so it will be visible as per the condition ie: getting from Database.
But when the page_Loads on that page - First rows shows correctly ie: when i click on paging for Second page - it shows error like this
I want to log all errors happened in my App to sql db. I'm passing all exception properties like (ex.Message, ex.Source, ex.StackTrace, ex.TargetSite and ex.InnerException) to stored procedure.
The problem is that when I try to pass the ex.InnerException as string to stored procedure I got this error:
Cannot implicitly convert type 'System.Exception' to 'string'
I tried ex.InnerException.ToString() and Convert.ToString (ex.InnerException) but no success.
I am getting Error Msg Given Below..After Uploading My WebApplication..whenever i try to access my webapplication? what is the exactly reason of this Error. Some time it solved automatically when i try some time later.
Server Error in '/' Application.
Exception of type 'System.OutOfMemoryException' was thrown. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
I recently downloaded the mvc 3 RC and I am receiving a type load exception everytime i do a fresh compile and start a debugging session. The error is
System.TypeLoadException crossed a native/managed boundary
[code]...
I can click ok and my debug session continues working as normal. I can close the session and restart without making any code changes, and it will open and work correctly. But, everytime I close, make a compilable change and rerun, I get the message.
I have ELMAH set up for a webapp, logging exceptions to a SQL server. I wish to have ELMAH send me an email too, but only when a specific exception is thrown (ie. MySpecialException). ELMAH must still log all exceptions to SQL server. I know you can do it programmatically in global.asax, but I'd prefer to use web.config. So, how do I restrict ELMAH error mails to filter out everything but a specific exception type, using web.config? UPDATE The filter ended up looking like this:
I am getting this exception whenever i tried to delete the WCF Service reference in project.I cannot even update it.Our WCF Service is bit huge and looks like this is causing the issue.I Start/Stopped IIS web server but still i cant get rid of it.