【问题标题】:Foreach through IEnumerable in view在视图中通过 IEnumerable foreach
【发布时间】:2013-02-23 17:11:31
【问题描述】:

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

我的视图模型:

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>

我在所有模块下都有红线。特性? 我不明白,我做错了什么? 谢谢

【问题讨论】:

    标签: asp.net-mvc-3 ienumerable


    【解决方案1】:

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

    foreach (var module in Model.ScormModuleInfo)
    

    在您的控制器中,您传递ScormModuleViewModel 以查看不是List&lt;ScormModuleViewModel&gt;

    所以替换

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

    收件人:

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

    【讨论】:

    • 这就是我的想法,但是当我键入 .模型之后??
    • aaaaah 连那个都没看到,再看一眼!大声笑谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多