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