无法找到为MVC 5 DropDownListFor添加占位符的方法 [英] Cannot find a way to add a placeholder for an MVC 5 DropDownListFor

查看:60
本文介绍了无法找到为MVC 5 DropDownListFor添加占位符的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过搜索网络,并尝试使用我的代码进行其他操作.我知道如何为文本框添加一个占位符,但是如何为MVC 5 dropdownlistfor添加一个占位符呢?

I've tried searching the web and trying out different things with my code. I know how to add a placeholder for a textbox, but how about adding one for an MVC 5 dropdownlistfor?

我有以下代码,但不会将占位符放在下拉列表中.

I have the following code, but will not put the placeholder in the dropdownlist.

@Html.DropDownListFor(model => model.CustomerID, new SelectList(ViewBag.Customers, "CustomerID", "Email", null, new { htmlAttributes = new { @class = "form-control", @placeholder = "-Select-" } }))

完成此操作所需的语法是什么?

What would be the syntax I would need to accomplish this?

推荐答案

尝试一下:

@Html.DropDownListFor(model => model.CustomerID, 
    new SelectList(ViewBag.Customers, "CustomerID", "Email"), 
    "-- Please Select --", 
    new { htmlAttributes = new { @class = "form-control" } })

第三个重载可以是占位符"(optionLabel).

3rd overload can be the "placeholder" (optionLabel).

选择框没有像文本输入一样的占位符".

A select box doesnt have a "placeholder" like text inputs do.

这篇关于无法找到为MVC 5 DropDownListFor添加占位符的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆