I've been butting my head against this problem for months now, and I
know that there aren't a lot of MonoRail + Mono users out there, but I
thought I would see if anyone has any ideas.
I'm using a pretty recent trunk version of MonoRail+ActiveRecord on
mono, and I've run into this on mono 2.4, 2.6, and now even the daily
tarball from yesterday (2.7 basically). It works fine on
MS.NET. I
receive a System.InvalidCastException when running the form helper
select method on some activerecord objects. The objects are very
simple (basically an Id and a Name - shown below), but in certain
cases (and the cases seem random, but they are consistently on the
same objects) it is throwing this exception on mono.
Please help me! I feel like I'm missing something and just can't find
it.
Here is the full stack trace:
System.InvalidCastException: Cannot cast from source type to
destination type.
at
System.Reflection.MonoProperty.GetterAdapterFrame[SectionProxy,Int32]
(System.Reflection.Getter`2 getter, System.Object obj) [0x00000] in /
root/mono/build/mono-20100729/mcs/class/corlib/System.Reflection/
MonoProperty.cs:245
at System.Reflection.MonoProperty.GetValue (System.Object obj,
System.Object[] index) [0x00070] in /tmp/mono/build/mono-20100729/mcs/
class/corlib/System.Reflection/MonoProperty.cs:307
at Castle.MonoRail.Framework.Helpers.AbstractFormRelatedHelper
+ReflectionValueGetter.GetValue (System.Object instance) [0x00000] in
<filename unknown>:0
at
Castle.MonoRail.Framework.Helpers.DifferentTypeOperationState.CreateItemRepresentation
(System.Object current) [0x00000] in <filename unknown>:0
at Castle.MonoRail.Framework.Helpers.OperationState.get_Current ()
[0x00000] in <filename unknown>:0
at Castle.MonoRail.Framework.Helpers.FormHelper.GenerateSelect
(System.String target, System.Object selectedValue, IEnumerable
dataSource, IDictionary attributes) [0x00000] in <filename unknown>:0
at Castle.MonoRail.Framework.Helpers.FormHelper.Select
(System.String target, System.Object selectedValue, IEnumerable
dataSource, IDictionary attributes) [0x00000] in <filename unknown>:0
at CompiledViews.myviewname.Render () [0x00000] in <filename
unknown>:0
at Castle.MonoRail.Views.AspView.AspViewBase.Process () [0x00000] in
<filename unknown>:0
at Castle.MonoRail.Views.AspView.AspViewBase.GetContentViewContent
() [0x00000] in <filename unknown>:0
at Castle.MonoRail.Views.AspView.AspViewBase.Process () [0x00000] in
<filename unknown>:0
at Castle.MonoRail.Views.AspView.AspViewBase.GetContentViewContent
() [0x00000] in <filename unknown>:0
at Castle.MonoRail.Views.AspView.AspViewBase.Process () [0x00000] in
<filename unknown>:0
at Castle.MonoRail.Views.AspView.AspViewEngine.Process
(System.String templateName, System.IO.TextWriter output,
IEngineContext context, IController controller, IControllerContext
controllerContext) [0x00000] in <filename unknown>:0
at
Castle.MonoRail.Framework.Services.DefaultViewEngineManager.Process
(System.String templateName, System.IO.TextWriter output,
IEngineContext context, IController controller, IControllerContext
controllerContext) [0x00000] in <filename unknown>:0
at Castle.MonoRail.Framework.Controller.ProcessView () [0x00000] in
<filename unknown>:0
at mycontrollername.ProcessView () [0x00000] in <filename unknown>:
0
at Castle.MonoRail.Framework.Controller.RunActionAndRenderView ()
[0x00000] in <filename unknown>:0
Here is the Section model that the SectionProxy is being generated
from:
[ActiveRecord("sections",Lazy=true)]
public class Section : ActiveRecordValidationBase<Section> {
// Private variables
private int id;
private string name;
[PrimaryKey("id")]
public virtual int Id {
get { return id; }
set { id = value; }
}
[Property("name", NotNull=true)]
[ValidateNonEmpty("Please enter a name for the section")]
public virtual string Name {
get { return name; }
set { name = value.Trim(); }
}
}
And finally here is my view code generating the select:
<aspView:properties>
<%
IList<Section> sections = null;
%>
</aspView:properties>
<p><%=Helpers.Form.LabelFor("selectedSection", "Section:")%> <
%=Helpers.Form.Select("selectedSection", sections,
Helpers.Dict.CreateDict("text=Name", "value=Id", "firstoption=",
"firstoptionvalue=0"))%></p>
I have tried setting the selected value on the FormHelper.Select
method, but am having no luck whatsoever in finding what exactly is
going on. This view code is designed to have none selected (ie
selectedSection is a made up target which doesn't exist as an object),
but I have used other complex objects which try to set the properties
appropriately and sometimes succeed and sometimes throw exactly the
same error.
I should reiterate I think - it is consistently thrown on the same
objects (ie the object id is 17), but works fine for other values/
selections.
Thanks so much for all of your help!
-Brian