0

我正在显示用户列表。每个用户记录都有一个 TrainingEvent 对象属性(从许多 TrainingEvent 中选择)。我创建了一个视图模型来包含用户列表和 TrainingEvents 列表。

视图模型:

public class RegistrationViewModel
{
  public IEnumerable<Domain.Entities.User> Users { get; set; }
  public IEnumerable<SelectListItem> TrainingEvents { get; set; }
}

(注意:Domain.Entities.User 包含一个 TrainingEvent 对象属性)

控制器:

public ActionResult Dealership(int id)
{
  var model = new RegistrationViewModel
    {
      Users = repository.Find
             .Where(u => u.Dealer.DealerId == id).OrderBy(u => u.LastName),
                 TrainingEvents = repository.TrainingEvents.ToList()
                                  .Select(x => new SelectListItem
                                          {
                                            Text = x.Date.ToString() + " - " + x.LocationName,
                                            Value = x.TrainingEventId.ToString()
                                          })
    };

  return View("East", model);
}

视图中的绑定:

@foreach(Company.Domain.Entities.User usr in Model.Users)
  {
     <tr>
         <td>@usr.LastName</td>
         <td>@usr.FirstName</td>
         <td>@usr.JobDescription</td>
         <td>@Html.DropDownListFor(m => m.Users.FirstOrDefault(u => u.UserId == usr.UserId).TrainingEvent, Model.TrainingEvents)</td>
     </tr>
}

列出了用户,并且每个用户行都有一个填充有 TrainingEvents 的下拉列表。但是,之前保存在用户的 TrainingEvent 对象中的值未被选中。任何想法为什么不?

4

1 回答 1

3

您错误地使用了DropDownListFor帮助程序。这个助手期望作为第一个参数传递一个简单的 lambda 表达式,最多包含一个成员访问。FirstOrDefault在您的示例中,您正在尝试使用不受支持的扩展方法之类的东西来构造一些复杂的 lambda 表达式。此外,从模型中获取一些数据也不是视图的责任。控制器有责任为视图填充合适的视图模型并将其传递给消费

因此,我建议您使用反映视图要求的真实视图模型:

public class UserViewModel
{
    public int TrainingEventId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string JobDescription { get; set; }
}

public class RegistrationViewModel
{
    public IList<UserViewModel> Users { get; set; }
    public IEnumerable<SelectListItem> TrainingEvents { get; set; }
}

这将在您的控制器操作中填充:

public ActionResult Dealership(int id)
{
    var users = repository
        .Find
        .Where(u => u.Dealer.DealerId == id)
        .OrderBy(u => u.LastName)
        .Select(u => new UserViewModel
        {
            TrainingEventId = u.TrainingEvent.TrainingEventId,
            FirstName = u.FirstName,
            LastName = u.LastName,
            JobDescription = u.JobDescription
        })
        .ToList();

    var trainingEvents = repository
        .TrainingEvents
        .ToList()
        .Select(x => new SelectListItem
        {
            Text = x.Date.ToString() + " - " + x.LocationName,
            Value = x.TrainingEventId.ToString()
        });

    var model = new RegistrationViewModel
    {
        TrainingEventIds = TrainingEventIds,
        TrainingEvents = trainingEvents
    };

    return View("East", model);
}

最后在您的视图中将DropDownListFor助手绑定到视图模型中的相应值:

@for(var i = 0 ; i < Model.TrainingEventIds; i++)
{
    <tr>
        <td>@Html.DisplayFor(m => m.Users[i].LastName)</td>
        <td>@Html.DisplayFor(m => m.Users[i].FirstName)</td>
        <td>@Html.DisplayFor(m => m.Users[i].JobDescription)</td>
        <td>
            @Html.DropDownListFor(
                m => m.Users[i].TrainingEventId, 
                Model.TrainingEvents
            )
        </td>
    </tr>
}
于 2012-11-04T13:47:03.083 回答