DropDownListFor with different label text
See original GitHub issueI 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:
- Created 10 years ago
- Comments:9 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I believe the functionality you are looking for is achieved through extension method
.Label()
:Let me know if this works out for you.
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 takesoptionLabel
. 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:
As for the label’s
for
attribute, you can use htmlAttributes to set it:Important: Notice uppercase “F” in the “For”. It is needed to override default “for” value.