DropDownList在MVC3中获取多个值

时间:2013-07-30 11:00:38

标签: asp.net-mvc-3 drop-down-menu

我正在开发一个在MVC3中创建的会员应用程序。我有两种不同的成员组织个人。在他们支付会员费后,我需要跟踪他们的付款记录。因此,在第一行(文字会员之后的字段)中,我需要显示个人组织和“MemberForname”的“OrganisationName” >在DropDownList中。但现在我只能显示“OrganisationName”或“MemberForname”。我尝试了很多,但仍然无法通过显示两者来弄明白。有任何想法吗?在此先感谢。[上面的屏幕截图仅显示“OrganisationName”,底部的屏幕仅显示“MemberForname”]。

        <td>
            Member
        </td>
        <td>
            @Html.DropDownList("member", ((IEnumerable<ProActiveMembership.Areas.Members.Models.Member>)ViewBag.PossibleMembers).Select(option => new SelectListItem
            {
                Text = Html.DisplayTextFor(_ => option.Forename).ToString(),
                //Text = Html.DisplayTextFor(_ => option.OrganisationName).ToString(),

                Value = option.MemberID.ToString(),
                Selected = (Model != null) && (option.MemberID == Model.MemberID)
            }), "Choose...", new { id = "ddl" })
            @Html.ValidationMessageFor(model => model.MemberID)
        </td>

enter image description here

1 个答案:

答案 0 :(得分:3)

您可以像这样设置Text属性:Text = string.Format("{0} {1}", option.Forename, option.OrganisationName)

您的代码将如下所示:

<td>
    Member
</td>
<td>
    @Html.DropDownList("member", ((IEnumerable<ProActiveMembership.Areas.Members.Models.Member>)ViewBag.PossibleMembers).Select(option => new SelectListItem
    {
        Text = string.Format("{0} {1}", option.Forename, option.OrganisationName),
        Value = option.MemberID.ToString(),
        Selected = (Model != null) && (option.MemberID == Model.MemberID)
    }), "Choose...", new { id = "ddl" })
    @Html.ValidationMessageFor(model => model.MemberID)
</td>

正如andreister建议您还可以向ProActiveMembership.Areas.Members.Models.Member添加一个返回全名的成员。像这样:

public string FullName { get { return string.Format("{0} {1}", Forename, OrganisationName); } }

然后您可以像这样使用新成员:

Text = option.FullName