Returning data from a viewmodel to one ActionResult with ASP.NET MVChttp://forums.asp.net/t/1708131.aspx/1?Returning+data+from+a+viewmodel+to+one+ActionResult+with+ASP+NET+MVCMon, 08 Aug 2011 18:13:00 -040017081314543188http://forums.asp.net/p/1708131/4543188.aspx/1?Returning+data+from+a+viewmodel+to+one+ActionResult+with+ASP+NET+MVCReturning data from a viewmodel to one ActionResult with ASP.NET MVC <p>I'm an ASP.NET MVC newbie and got stuck trying to trying to return data from a viewmodel into one ActionResult.</p> <p>Here's the model:</p> <pre class="prettyprint">public class SitePage { public int ID { get; set; } public string Name { get; set; } public int? SiteId { get; set; } public string Url { get; set; } [ForeignKey(&quot;SiteId&quot;)] public SitePage Page { get; set; } }</pre> <p>and the viewmodel:</p> <pre class="prettyprint">public class SitePageViewModel { public SitePage DomainPages { get; set; } public SitePage ContentPages { get; set; } }</pre> <p>I've got this in my controller:</p> <pre class="prettyprint">public ViewResult Index() { var domainPages = (from d in db.SitePages where d.SiteId == null select d).ToList(); var contentPages = (from c in db.SitePages where c.SiteId != null select c).ToList(); return View(new SitePageViewModel {DomainPages = domainPages, ContentPages = contentPages }); }</pre> <p>I can't convert the lists to the correct data type for return in the view, what am I missing?</p> 2011-08-07T20:01:49-04:004543219http://forums.asp.net/p/1708131/4543219.aspx/1?Re+Returning+data+from+a+viewmodel+to+one+ActionResult+with+ASP+NET+MVCRe: Returning data from a viewmodel to one ActionResult with ASP.NET MVC <pre class="prettyprint">public ViewResult Index() { var Model=new SitePageViewModel (); Model.domainPages=new SitePage(); // 0r retrieve somehow from database</pre> <pre class="prettyprint"><br /><br />&nbsp;return View(Model); }</pre> 2011-08-07T21:31:59-04:004543229http://forums.asp.net/p/1708131/4543229.aspx/1?Re+Returning+data+from+a+viewmodel+to+one+ActionResult+with+ASP+NET+MVCRe: Returning data from a viewmodel to one ActionResult with ASP.NET MVC <p>Thanks, I've changed the controller but now run into a type conversion problem when I try to get the data from the database.</p> <pre class="prettyprint">public ViewResult Index() { var Model = new SitePageViewModel(); Model.DomainPages = (from d in db.SitePages where d.SiteId == null select d).ToList(); Model.ContentPages = (from c in db.SitePages where c.SiteId != null select c).ToList(); return View(Model); }</pre> <p>If I use ToList() I get a &quot;Cannot implicitly convert type 'System.Linq.IQueryable ...&quot; error and if leave out the ToList() I get an error in my view which is expecting an IEnumerable type.</p> 2011-08-07T22:19:08-04:004543254http://forums.asp.net/p/1708131/4543254.aspx/1?Re+Returning+data+from+a+viewmodel+to+one+ActionResult+with+ASP+NET+MVCRe: Returning data from a viewmodel to one ActionResult with ASP.NET MVC <p>in your Model, DomainPages and ContntPages are not collections.</p> <p></p> 2011-08-08T00:16:47-04:004544747http://forums.asp.net/p/1708131/4544747.aspx/1?Re+Returning+data+from+a+viewmodel+to+one+ActionResult+with+ASP+NET+MVCRe: Returning data from a viewmodel to one ActionResult with ASP.NET MVC <p>OK, I've changed the Model so that DomainPages and ContentPages are IEnumerable and when I run in debug mode and check the controller they are getting populated correctly.</p> <p>I've got @model IEnumerable at the start of my view but I'm still getting an error <br> <i>The model item passed into the dictionary is of type 'admTest.Models.SitePageViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[admTest.Models.SitePageViewModel]'.</i> </p> <pre class="prettyprint">Model public class SitePageViewModel { public IEnumerable&lt;SitePage&gt; DomainPages { get; set; } public IEnumerable&lt;SitePage&gt; ContentPages { get; set; } } Controller public ViewResult Index() { var Model = new SitePageViewModel(); Model.DomainPages = (from d in db.SitePages where d.SiteId == null select d).ToList(); Model.ContentPages = (from c in db.SitePages where c.SiteId != null select c).ToList(); return View(Model); } View @model IEnumerable&lt;admTest.Models.SitePageViewModel&gt;</pre> 2011-08-08T17:30:58-04:004544790http://forums.asp.net/p/1708131/4544790.aspx/1?Re+Returning+data+from+a+viewmodel+to+one+ActionResult+with+ASP+NET+MVCRe: Returning data from a viewmodel to one ActionResult with ASP.NET MVC <p>I think I've worked it out - changed the reference in my view to @model admTest.Models.SitePageViewModel</p> 2011-08-08T18:13:00-04:00