Html helper method for DropDownList to add title tag for each list items

StackOverflow https://stackoverflow.com/questions/23499789

  •  16-07-2023
  •  | 
  •  

سؤال

I want to create Html helper method to add title html attribute for each items in DropDownListFor control.

 @Html.DropDownListFor(m=> m.StateId, Model.States)

 public class Model
 {
    public int? StateId {get; set;}
    public List<SelectListItem> States {get; set;}
 }

 In controller,
  Model m = new Model();
  m.States = GetStates(). This will return List<SelectListItem> of States with Value and Text.

Now I want to create html helper method to add title attribute for each item with text value.

هل كانت مفيدة؟

المحلول

You will need to create on your own.

You can pull from similar implementation Adding html class tag under <option> in Html.DropDownList

Also give a try to

@Html.DropDownListFor(model => model.priority, new SelectList(ViewBag.Priority, "Value", "Text"), "-- Select Priority --", new { @class = "required", title="priority" })
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top