Since it is not possible to pass multiple models to a view in ASP.NET MVC 4, I am trying to stuff the various models into a dynamic ExpandoObject
and then unpacking it from within the view.
My Model (consists of more than just this class, but for brevity I'll just show this):
public class Modular_ArtistModel
{
public string Artist_Name { get; set; }
}
My Controller:
(I am packing more than just this List<>
object into the dynamic
object, but for brevity's sake...)
dynamic ArtistModel = new ExpandoObject();
var Modular_ArtistModel = LoadSP_Modular_ArtistModel("sp_Mod_Artist_Artist", i);
List<Modular_ArtistModel> mod_ArtistModel = new List<Modular_ArtistModel>();
foreach (var row in Modular_ArtistModel)
{
mod_ArtistModel.Add(new Modular_ArtistModel
{
Artist_Name = row.Artist_Name
});
}
ArtistModel.Artist = mod_ArtistModel;
My View: (This is the first thing in the view and the program chokes on the following assignment)
@model dynamic
@{
string artist_Name = Model.Artist.Artist_Name;
}
When the cursor reaches the above assignment in the View layer, it throws the following exception:
'Model.Artist.Artist_Name' threw an exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233088
HelpLink: null
InnerException: null
Message: "'System.Collections.Generic.List<....Models.Modular_ArtistModel>' does not contain a definition for 'Artist_Name'"
Source: "Anonymously Hosted DynamicMethods Assembly"
StackTrace: " at CallSite.Target(Closure , CallSite , Object )\r\n at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)"
TargetSite: {System.Object CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, System.Object)}
Does anyone know what I need to do to fix this? Not sure if it's a quick fix or a more extensive redesign.