代码之家  ›  专栏  ›  技术社区  ›  Yoda ESG

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

  •  1
  • Yoda ESG  · 技术社区  · 11 年前

    编辑并部分回答: 我不知道如何显示多个列,但如果不是这样,则必须在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))
    

    如果我只是在 呼叫 :

       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 回复  |  直到 11 年前
        1
  •  2
  •   user3559349 user3559349    11 年前

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

    查看模型

    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>