Foreach通过IEnumerable进行查看

时间:2013-03-08 07:56:37

标签: asp.net-mvc-3 ienumerable

有人可以帮忙,不知道我做错了什么:

我的viewmodel:

public class ScormModuleViewModel
{
  public IEnumerable<ScormModuleInfo> ScormModuleInfo { get; set; }
  public int CurrentPage { get; set; }
  public int PageSize { get; set; }
  public double Total { get; set; }
  public int RecordsPerPage { get; set; }
}

然后我的控制器:

public ActionResult Index(int? page)
{
  List<ScormModuleInfo> modules = new List<ScormModuleInfo>();
  foreach (var directory in Directory.EnumerateDirectories(scormRootDir))
  {
     ScormModuleInfo module = new ScormModuleInfo();
     module.ScormModule = ZincService.ScormService.GetScormModule(scormTitle, scormDirectory);

      if (module.ScormModule != null)
        module.Installed = true;
      else
      {
        module.Installed = false;
        module.ScormModule = new ScormModule();
        module.ScormModule.Directory = scormDirectory;
        module.ScormModule.Title = scormTitle;
      }

      module.ScormModule.EntryPointRef = scormEntryPointRef;
      module.ScormModule.IdentifierRef = scormIdentifierRef;
      module.ScormModule.LaunchHeight = scormLaunchHeight;
      module.ScormModule.LaunchWidth = scormLaunchWidth;
      module.ScormModule.LaunchResize = scormLaunchResize;
      module.ScormModule.RelativeHtmlPath = scormRelativeHtmlPath;
      module.ScormModule.SchemaVersion = scormSchemaVersion;

      if (module.Installed)
      {
        // TODO: Save changes to module
        //ZincService.ScormService.UpdateScormModuleSettings(
      }

      modules.Add(module);
      noOfScormPackages++ ;

    }
  }

  ScormModuleViewModel model = new ScormModuleViewModel();
  model.ScormModuleInfo = modules;
  model.CurrentPage = page.GetValueOrDefault(1);
  model.PageSize = PageSizeSettings.ScormPackages;
  model.Total = noOfScormPackages;

  // Now iterate over modules and read imsmanifest.xml file for details of entry point and resources required.

  return View(model);
 }

和我的观点:

 <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<Zinc.Web.Areas.Admin.ViewModels.Scorm.ScormModuleViewModel>>" %>

      foreach (var module in Model)
   { %>
 <li>
   <div class="col1">
     <a href='javascript:LoadTrainingModuleWithDedicatedAPIObj("<%= module.ScormModuleInfo.r   .sc.ScormModule.RelativeHtmlPath %>/<%= module.ScormModule.EntryPointRef %>", "<%: module.ScormModule.Title %>", "<%: module.ScormModule.LaunchWidth %>", "<%: module.ScormModule.LaunchHeight %>");'>
       <% if (module.ScormModule.Title.Length < 70)
          { %>
         <%: module.ScormModule.Title%>
       <% }
          else
          { %>
          <%: module.ScormModule.Title.Substring(0, 70)%>....
       <% } %>
     </a>
   </div>

我在所有模块下获得红线。属性? 我不明白,我做错了什么? 感谢

1 个答案:

答案 0 :(得分:0)

Model是您的ViewModel。 Model.ScormModuleInfoScormModuleInfo类型的IEnumerable。

foreach (var module in Model.ScormModuleInfo)

在您的控制器中,您传递ScormModuleViewModel以查看不List<ScormModuleViewModel>

所以替换

System.Web.Mvc.ViewPage<List<Zinc.Web.Areas.Admin.ViewModels.Scorm.ScormModuleViewModel>>

要:

System.Web.Mvc.ViewPage<Zinc.Web.Areas.Admin.ViewModels.Scorm.ScormModuleViewModel>