question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DropDownListFor with different label text

See original GitHub issue

I have some classes

class Foo 
{
    public int id;
    public int barId;
    public virtual Bar Bar;
}

class Bar 
{
    public int id;
    [Display(Name = "BarDispName")]
    public string name;
}

public class FooMap : EntityTypeConfiguration<Foo>
{
    public FooMap()
    {
        // Primary Key
        this.HasKey(t => t.id);

        // Relationships
        this.HasRequired(t => t.Bar)
            .WithMany(t => t.Foos)
            .HasForeignKey(d => d.barId);
    }
}

The closes thing I need is

@Html.Bootstrap().ControlGroup().DropDownListFor(m => m.barId, ViewBag.barId as IEnumerable<SelectListItem>)

The problem is that generated label is ‘bar Id*’, but I want it to be the same as

@Html.LabelFor(m => m.Bar.name)

plus ‘*’ if needed.

So I suggest to add an overload to DropDownListFor with two expressions:

  • For ‘Required’ rules (adding *) and for attributes like

    label.for, select.{id, name, data-val-required, data-val-number}
    
  • For text of the label

If there is a way to do it now, I would be glad to know about it.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
DmitryEfimenkocommented, Aug 8, 2013

I believe the functionality you are looking for is achieved through extension method .Label():

@(Html.Bootstrap().ControlGroup().DropDownListFor(m => m.barId, ViewBag.barId as IEnumerable<SelectListItem>)
    .Label().LabelText(Model.Bar.name).ShowRequiredStar(true))

Let me know if this works out for you.

0reactions
DmitryEfimenkocommented, Aug 9, 2013

I still do not quite understand the whole thing that you are trying to do. The whole thing feels to be a bit wrong if you need to write so much customization. Though I don’t care that much 😃

The verdict is that if you can construct this with the use of regular html helpers, you should be able to achieve the same with BMVC’s custom control.

Based on the code you showed me I tried to reproduce the same logic with BMVC, but found that BMVC .DropDownList method does not have overload that you need. The one that does not require List<SelectListItem> and takes optionLabel. This is a bug and it will be fixed in upcoming release (which should happen today or tomorrow).

When it’s fixed you would be able to achieve the same by writing this:

@(Html.Bootstrap().ControlGroup()
   .CustomControls(Html.Bootstrap().DropDownList("makerId", ""))
   .LabelFor(m => m.Maker.name))

As for the label’s for attribute, you can use htmlAttributes to set it:

// BMVC:
.LabelFor(m => m.Maker.name).HtmlAttributes(new { @For = "makerId" })

// Regular helper:
@Html.LabelFor(m => m.Maker.name, new { @For = "makerId" })

Important: Notice uppercase “F” in the “For”. It is needed to override default “for” value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

asp.net mvc - How do I display a different label text from the ...
Currently my oracle table has values Y/N for Enable and Disable. When editing the row the dropdownlist shows, Enable and Disable. <div class=" ......
Read more >
Solved: .NET MVC - Changing the value of a label based ...
Using the ID from the dropdown to pull other values from the database then do math to produce the final value. This is...
Read more >
Changing label text from a database from a dropdownlist ...
I am trying to change a labels text based on what the users selects from a dropdownlist which is populated from a sql...
Read more >
Binding Dropdownlist With Database In MVC
This article shows how to bind a dropdownlist in various ways with a database.
Read more >
ASP.NET Core DropDownList Floating Label - Demos - Telerik
NET Core DropDownList component supports the Floating Label feature—a placeholder text for form or input fields, which floats above that field and remains....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found