如何在Html.DropDownListFor上选择显示的文本 [英] How to choose displayed text on Html.DropDownListFor

查看:158
本文介绍了如何在Html.DropDownListFor上选择显示的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为某些类型选择在Html.DropDownListFor中显示哪个属性?

How can I choose which property is displayed in Html.DropDownListFor for a some type?

例如,我有一个我要从中选择值的类

For example I have the following class from which I want to select values

public partial class Artysta
    {
        public Artysci()
        {
            this.SpektaklArtysta = new HashSet<SpektaklArtysta>();
        }

    [Key]
    public int ArtystaID { get; set; }

    public string Imie { get; set; }

    public string Nazwisko { get; set; }        
}

这是为编辑视图生成的代码,有时会显示 Imie ,有时会显示 Nazwisko .

Here is a generated code for edit View which sometimes displays Imie and from time to time Nazwisko.

@using (Html.BeginForm())
{
@model Teatr.Models.SpektaklArtysta
<div class="form-group">
            @Html.LabelFor(model => model.ArtystaID, "Artysta", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ArtystaID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ArtystaID, "", new { @class = "text-danger" })
            </div>
        </div>
}

我想将显示的属性设置为 Nazwisko ,如何实现?

I would like to set displayed property to Nazwisko, how can I achieve that?

更新:

这是生成此视图的实际模型.

Here is actual model that this View was generated.

public partial class SpektaklArtysta
    {
        public int SpektaklID { get; set; }

        public int ArtystaID { get; set; }

        public int RolaArtystyID { get; set; }

        [Key]
        public int SpektaklArtystaID { get; set; }

        public virtual Artysci Artysci { get; set; }

        public virtual RolaArtysty RolaArtysty { get; set; }

        public virtual Spektakle Spektakle { get; set; }
    }

推荐答案

好,您实际上需要将可能值的列表传递给下拉列表,例如:

Ok, you need to actually pass a list of possible values to the dropdown list, for example like this:

@Html.DropDownListFor(model => model.ArtystaID, new SelectList(Model.Artysci, "ArtystaID", "Nazwisko", 0))

它说:DropDownList设置模型ArtystaID字段,该字段由模型Artysci字段填充,该字段应包含在ArtystaID字段下具有键并在Nazwisko字段下显示文本的项目(因此,您的Artysta类).

It says: DropDownList setting models ArtystaID field, which is populated by models Artysci field, which is supposed to contain items that have key under ArtystaID field and display text under Nazwisko field (so, your Artysta class).

现在,您的班级没有Artysci字段,因此您必须创建它:

Now, your class doesn't have Artysci field, so you have to create it:

public partial class Artysta
    {
        public Artysci()
        {
            this.SpektaklArtysta = new HashSet<SpektaklArtysta>();
        }

    [Key]
    public int ArtystaID { get; set; }

    public string Imie { get; set; }

    public string Nazwisko { get; set; }      

    public List<Artysta> Artysci = ArtistRepository.GetAllArtists();
}

或者您可以直接在DropDownList中传递它:

Or you can pass it directly in DropDownList:

@Html.DropDownListFor(model => model.ArtystaID, new SelectList(ArtistRepository.GetAllArtists(), "ArtystaID", "Nazwisko", 0))

哦,还有一个私人提示:我知道这可能不是您的选择,除非您是首席/唯一开发人员,但是请为您的类,变量等使用英文名称,将来如果使用它会更容易其他人将不得不处理您的代码,而他们可能不会讲波兰语;)

Oh, and just a personal note: I know it's probably not your choice, unless you are a lead/sole developer, but please use English names for your classes, variables etc., it will be much easier if in the future someone else will have to work on your code, and they might not speak Polish ;)

这篇关于如何在Html.DropDownListFor上选择显示的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆