繁体   English   中英

如何使用@Html.DropDownListFor 为 asp.net MVC 3 中的 SelectList 描述组合两个 dataTextFields

[英]How to combine two dataTextFields for SelectList Description in asp.net MVC 3 using @Html.DropDownListFor

我正在返回一些存储的联系人以查看 DropDownList,但我无法在我的 SelectList 中包含多个 dataTextFields。

目前我有:

@Html.DropDownListFor(model => model.Account.AccountContacts, 
         new SelectList(ViewBag.DDContacts, "Contact.ContactID", "Contact.FirstName"),   
         new { @class = "select", style = "width: 100px;" })

我想拥有:

@Html.DropDownListFor(model => model.Account.AccountContacts, 
        new SelectList(ViewBag.DDContacts, "Contact.ContactID", "Contact.FullName"),         
        new { @class = "select", style = "width: 100px;" })

它结合了 FirstName 和 LastName 属性。

更新:我知道扩展联系人属性,我很好奇是否有一种简化的方法可以在视图或 Controller 中完成此操作。我在这里尝试了两种解决方案但无济于事。 如何在 SelectList 文本描述中组合两个字段?

扩大联系

public partial class Contact
{
   private string _nameFull;
   public string NameFull
   { 
      get{return FirstName + " " + LastName;}
   }
}

我意识到这个问题有一个批准的答案,但我不相信这是提问者想要的。 我遇到了这个问题,这就是我解决它的方法:

@Html.DropDownListFor(model => model.Account.AccountContacts, 
     new SelectList((from c in ViewBag.DDContacts.ToList() select new {
         ID_Value = c.Contact.ContactID,
         FullName = c.Contact.FirstName + " " + c.Contact.LastName
     }), "ID_Value", "FullName"),   
     new { @class = "select", style = "width: 100px;" })

工作原理:我们使用列表中的 linq 到 select 特定数据,并将其放入新的 object 中。这个新的 object 将仅包含两个属性,“ID_Value”和“FullName”。 现在我们的 DropDownListFor 可以使用这个新的 object 来实现我们想要的 output。

我做了这样的事情:

Controller:

ViewBag.Regiones = from p in modeloDB.Regiones.ToList() select new {
    Id = p.Id,
    Nombre = p.Codigo + " - " + p.Nombre
};

在 View 中,我是这样做的:

@Html.DropDownListFor(model => model.Region,
new SelectList(@ViewBag.Regiones, "Id", "Nombre"),
new { @class = "comboBox" })
@Html.ValidationMessageFor(model => model.Region)

它非常适合我! 我希望它仍然有帮助!

Go 到您的 Model 并创建一个具有相同实体名称的新联系人 class。 声明命名空间时要小心。
这里有一些代码会有所帮助(我希望):

public partial class Contact
{ 
   [DataMember]
   public string FullName{ get;set;}
}

在您的 controller 中,只需执行以下操作:

data.FullName =data.Firstname+" "+data.LastName;

应该这样做。

向您的Contact class 添加一个属性,该属性返回FullName但考虑到 null,空的或空格的名字或姓氏:

[DataMember]
public string FullName
{
    return string.Join(
        " ",
        new string[] { this.FirstName, this.LastName }
            .Where(s => !string.IsNullOrWhiteSpace(s))));
}

这是我会怎么做的。 只需修改代码以适合您的场景。 我只是为了演示目的而更改我的代码。

在我看来,我会有以下内容:

@model MyProject.ViewModels.MyViewModel

<table>
     <tr>
          <td><b>Bank:</b></td>
          <td>
               @Html.DropDownListFor(
                    x => x.BankId,
                    new SelectList(Model.Banks, "Id", "IdAndName", Model.BankId),
                    "-- Select --"
               )
               @Html.ValidationMessageFor(x => x.BankId)
          </td>
     <tr>
</table>

我的观点model:

public class MyViewModel
{
     public int BankId { get; set; }
     public IEnumerable<Bank> Banks { get; set; }
}

我的银行 class:

public class Bank : IEntity
{
     public int Id { get; set; }
     public string Name { get; set; }
     public bool IsActive { get; set; }

     // This property will bring back the concatenated value.
     // If the Id is 1 and Name is My Bank,
     // then the concatenated result would be 1: My Bank
     public string IdAndName
     {
          get
          {
               return (Id.ToString() + ": " + Name);
          }
     }
}

在我的 controller 行动中:

public ActionResult MyActionMethod()
{
     MyViewModel viewModel = new MyViewModel
     {
          Banks = bankService.GetAll()  // Database call to get all the banks
     };

     return View(viewModel);
}

我希望这有帮助。

向您的联系人 class 添加一个名为 FullName 的属性,它只返回 FirstName + " " + LastName。

您可以按原样从数据库中获取联系人...并使用 LINQ 您可以查询第一个集合,返回另一个具有 ID 和 FULLNAME 的集合...然后使用此集合填充您的下拉列表。

那只是为了满足您的好奇心……我认为最好的方法是为简单起见扩展您的 Contact ViewModel。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM