如何将select html标签转换为Html.DropDownList?

时间:2016-07-09 18:50:13

标签: jquery html asp.net-mvc html5 razor

我如何将html中的此标记转换为@ Html.DropDownList?

 <select class="selectpicker input-price" data-live-search="true" data-width="100%"
   data-toggle="tooltip" title="Select">
    <option>Select City</option>
    <option>Test1</option>
    <option>Test2</option>
</select>

我无法将数据实时搜索和数据宽度设置为@ Html.DropDownList

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

这就是我正在为下拉列表做的事情:

控制器代码

public ActionResult Contact()
{
    Contact homeViewModel = new Contact();
    homeViewModel.subject = new List<Subject>();
    homeViewModel.subject.Add(new Subject { Id = 1, subject1 = "General Customer Service" });
    homeViewModel.subject.Add(new Subject { Id = 2, subject1 = "Suggestions" });
    homeViewModel.subject.Add(new Subject { Id = 3, subject1 = "Product Support" });

    homeViewModel.Selectedid = 1; //this sets the default value for your dropdown

    return View(homeViewModel);
}

型号代码

public class Contact
{   
  [Required(ErrorMessage="Please fill the following")]
  public string name { get; set; }
  [Required(ErrorMessage = "Please Enter Correct Username")]
  [RegularExpression(@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$", ErrorMessage = "Please provide valid email id")]
  public string email { get; set; }
  public List<Subject> subject { get; set; }
  [Required(ErrorMessage="Please fill the following")]
  public string message { get; set; }
  public int Selectedid { get; set; }
}

查看代码

 @model programname.model.Contact

 @Html.DropDownList("Selectedid", new SelectList(Model.subject, "Id",    "subject1", Model.Selectedid), new { @class = "form-control", id = "subject", required = "required" }