How to choose displayed text on Html.DropDownListF

2019-08-02 02:49发布

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; }        
}

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>
}

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

UPDATE:

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; }
    }

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-02 03:05

One good option for you is to use the DropDownList as following

@Html.DropDownList("DropDownId", Model.Select(item => new SelectListItem
{
    Value = item.ArtystaID.ToString(),
    Text = item.Nazwisko.ToString(),
     Selected = "select" == item.ArtystaID.ToString() ? true : false
}))

Hope that answers your request!

查看更多
劫难
3楼-- · 2019-08-02 03:10

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))

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).

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();
}

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 ;)

查看更多
登录 后发表回答