显示带有一个附加属性的关联类的Html.ListBoxFor

时间:2014-09-07 00:14:15

标签: c# asp.net-mvc linq entity-framework razor

编辑和部分答案:我不知道如何显示多个列但如果不是这样你必须覆盖Call类中的ToString。


我有一个班级Person(他们接听电话):

 public class Person {
        public int Id { get; set; }

        [DisplayName("First Name")]
        public string FirstName { get; set; }

        [DisplayName("Last Name")]
        public string LastName { get; set; }

        public virtual ICollection<Call> ReceivedCalls { get; set; }
    }

班级ApplicationUser(他们拨打电话):

 public class ApplicationUser : IdentityUser {
        public virtual ICollection<Call> MadeCalls { get; set; }
 }

和它们之间的关联类Call

 public class Call {
        public int Id { get; set; }
        public virtual Person Callee { get; set; }
        public virtual ApplicationUser Caller { get; set; }
        public DateTime TimeStamp { get; set; } //Additional Attribute
    }

他们在数据库中一起工作很棒。但是我使用@Html.ListBoxFor方法显示一个ListBox时遇到问题,其中每一行都会有一些信息,当收到的每个电话都是Caller.UserName | Call.TimeStamp时发出的信息。

我想在Razor View中强烈显示这些信息:

@model WebApplication2.Models.Person

我试过这个:

@Html.ListBoxFor(m => m.ReceivedCalls, new MultiSelectList(Model.ReceivedCalls, "TimeStamp" ), new {Multiple = "multiple"})

正在翻译(输出HTML)到:

<select Multiple="multiple" id="ReceivedCalls" multiple="multiple" name="ReceivedCalls"><option selected="selected">System.Data.Entity.DynamicProxies.Call_2FC3E1C25C19BA21E6B64CAB2894602434775435359AAB76E58C6F8411451A49</option>
<option selected="selected">System.Data.Entity.DynamicProxies.Call_2FC3E1C25C19BA21E6B64CAB2894602434775435359AAB76E58C6F8411451A49</option>
</select>

看起来像:

enter image description here

问题:如何显示ListBox,其中每一行都会有被调用的信息以及何时:Caller.UserName | Call.TimeStamp为每次收到的电话显示(按人员)。


**编辑:**

有一些语法错误:

        // Create collection of anonymous objects (adjust format to suit your needs)
        var receivedCalls = person.ReceivedCalls.Select(c => new {
            ID = c.Id,
            DisplayName = string.Format("{0}: {1:D}", c.Caller.UserName, c.TimeStamp)
        });
        // Initialise view model
        PersonVM model = new PersonVM();
        // Map properties from person to model

        // Assign select list
        model.CallList = new MultiSelectList(receivedCalls, "ID", "DisplaName");

查看:

@Html.ListBoxFor(m => m.ReceivedCalls, new MultiSelectList(Model.ReceivedCalls))

如果我只是覆盖Call中的ToString:

,结果会非常相似
   public override string ToString() {
            if (Caller != null)
                return Caller.UserName + " called at " + TimeStamp;
            return "called at " + TimeStamp;
        }

@Html.ListBoxFor(m => m.ReceivedCalls, new MultiSelectList(Model.ReceivedCalls, "TimeStamp"), new { Multiple = "multiple", style = "width: 100%;" })

所以我认为它仍然依赖于ToString。

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该创建一个视图模型来显示和绑定您的属性

查看模型

public class PersonVM
{
  public int Id { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public int[] ReceivedCalls { get; set; } // this will bind to the selected calls
  public MultiSelectList CallList { get; set; } // to display the calls   
}

控制器

// Create collection of anonymous objects (adjust format to suit your needs)
var receivedCalls = person.ReceivedCalls.Select(c => new 
{
  ID = c.ID,
  DisplayName = string.Format("{0}: {1:D}", c.Callee.UserName, c.TimeStamp)
}
// Initialise view model
PersonVM model = new PersonVM();
// Map properties from person to model
...
// Assign select list
model.CallList  = new MultiSelectList(receivedCalls , "ID", "DisplaName");
return View(model);

查看

@model YourAssembly.PersonVM
....
@Html.ListBoxFor(m => m.ReceivedCalls, Model.CallList)

修改

根据OP的要求,即最近调用的集合仅用于显示(不用于编辑),请调整视图以使用无序列表(基于OP的模型,而不是建议的视图模型)以上)

@model WebApplication2.Models.Person
....
<ul>
  @foreach(var call in Model.ReceivedCalls)
  {
    <li>@call</li> // this will use the .ToString() method
  }
</ul>

然后根据需要设置元素样式,例如设置固定高度并使其可滚动而不显示项目符号

<style>
  ul {
    height:60px;
    overflow-y:scroll;
  }
  li {
    list-style:none;
  }
</style>