MVC 5 c#foreach over ViewBag包含View中的3个列表

时间:2014-11-01 01:10:23

标签: c# asp.net-mvc model viewbag

我有一个UserProfileModel,它包含标准的配置文件项和两个包含UserLanguages和UserPendingItems的类列表。我看过很多类似的问题,但对我没有帮助......

UserProfileModels

public class UserProfileModels
{
    [Key]
    public string UserProfileID { get; set; } // Same as User ID
    public string UserProfilePictureURL { get; set; }
    public string UserProfileCompanyID { get; set; } // Same as Company ID
    // IF UserProfileCompanyID is empty then EU is an individual and the following fields are available
    // Else Get the information from the CompanyProfile

    public string UserProfileStreet { get; set; }
    public string UserProfileTown { get; set; }
    public string UserProfileCountry { get; set; } // Allows us to decide on currency, TimeZone, State / County, Post Code / Zip etc
    public string UserProfileState { get; set; }
    public string UserProfilePostCode { get; set; }
    public string UserProfileDOB { get; set; } // Date Of Birth
    public string UserProfileSex { get; set; }

    public string UserProfileRole { get; set; }
    // IF Role is > "Client" then the following fields are to be used.
    public string UserProfileTelephoneNumber { get; set; }
    public string UserProfileMobileNumber { get; set; }

    public List<UserLanguageModels> UserLanguages { get; set; }

    public List<UserProfilePendingItems> UserPendingItems { get; set; }

}

UserProfilePendingItems

public class UserProfilePendingItems
{
    public string UserProfileID { get; set; }
    public string UserProfileDescription { get; set; }
}

UserLanguageModels

public class UserLanguageModels
{
    public string UserProfileID { get; set; }
    public string UserLanguage { get; set; }
}

在视图中,有一个下拉菜单,其中包含UsersPendingItems列表

查看

<ul class="dropdown-menu extended tasks-bar">
  <div class="notify-arrow notify-arrow-green"></div>
  <li>
    <p class="green">@ViewBag.PendingMessages.Count()</p>
  </li>

问题在这里开始

@foreach (CCSLABS.Controllers.Administration.AdministrationController.UserProfile Message in ViewBag.PendingMessages)
{                       
  <li>
    <a href="index.html#">
      <div class="task-info">
        <div class="desc">@Message.UserPendingItems.</div>
        <div class="percent">40%</div>
      </div>
      <div class="progress progress-striped">
        <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 40%">
         <span class="sr-only">40% Complete (success)</span>
        </div>
      </div>
    </a>
  </li>
}

因此,这个想法在用户的个人资料中,我们有一个他们需要做的项目列表(待定项目)。我们通过ViewBag发送到视图(可能有更好的方法来执行此操作!)我们对ViewBag进行Foreach并创建下拉菜单。

然而,

  1. 我最终会在视图中使用过多的代码逻辑,这使得它成为维护的噩梦。
  2. 无论如何,我无法查看 ViewBag中的PendingItemsModel。
  3. 问题

    1. 如何仅将用户拥有的前4个或5个PendingItem发送到View
    2. 如何使用视图中尽可能少的代码创建视图列表项的迭代以生成下拉菜单。

0 个答案:

没有答案