asp.net-mvc 指定名称和 ID 下拉剃刀技术 asp dot net mvc4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19332527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
specify name and id to Drop down razor technique asp dot net mvc4
提问by Rahil
I have asp web form and i'm using razor technique to post that form, problem is that, i want to know that how to specify ID and NAME attribute to razor dropdown list because, in next step, i'm getting the form value by using the ID and NAME attribute..
我有 asp 网络表单,我正在使用 razor 技术来发布该表单,问题是,我想知道如何为 razor 下拉列表指定 ID 和 NAME 属性,因为在下一步中,我正在获取表单值通过使用 ID 和 NAME 属性..
VIEW code is :
查看代码是:
@Html.DropDownList("ID", (SelectList) ViewBag.list, " -- Select Business Type -- ")
CONTROLLER:
控制器:
public ActionResult coverage()
{
var query = db.Database.SqlQuery<businessDropDownModel>("businesDropDownSP");
ViewBag.list = new SelectList(query.AsEnumerable(), "ID", "BusinessTypeName", "----select----");
return View();
}
MODEL:
模型:
public class businessDropDownModel
{
public int ID { set; get; }
public string BusinessTypeName { set; get; }
}
回答by Matt Bodily
If you want to set the name and id or other attributes you can use the html attributes option.
如果要设置名称和 ID 或其他属性,可以使用 html 属性选项。
NOTE: the method has a quirk: you can use the attributes dictionary to override the "id", but notthe "name".
注意:该方法有一个怪癖:您可以使用属性字典覆盖“id”,但不能覆盖“name”。
example:
例子:
@Html.DropDownList("Selected", Model.SelectList, "First Option", new { name = "Name", id = "id"})
will render:
将呈现:
<select id="id" name="Selected">...</select>
So the upshot is: if you want name and id to be different, set the name parameterto what you want the name to be, and use the dictionary to override the id.
所以结果是:如果您希望 name 和 id 不同,请将 name参数设置为您想要的名称,并使用字典覆盖 id。
回答by JLe
The first argument in @Html.DropDownList("MyIdAndName", (SelectList) ViewBag.list, " -- Select Business Type -- ")is both the id and the name of the selecttag.
第一个参数@Html.DropDownList("MyIdAndName", (SelectList) ViewBag.list, " -- Select Business Type -- ")是select标签的 id 和名称。
So,
所以,
@Html.DropDownList("MyIdAndName", (SelectList) ViewBag.list, " -- Select Business Type -- ")
would render
会呈现
<select id="MyIdAndName" name="MyIdAndName">...</select>
回答by randyh22
If you need to set the Id to something different than the Name, you can use the htmlAttributes parameter to set the Id of the control. You still use the first parameter in the DropDownList helper to set the Name of the control.
如果您需要将 Id 设置为与 Name 不同的内容,您可以使用 htmlAttributes 参数来设置控件的 Id。您仍然使用 DropDownList 帮助器中的第一个参数来设置控件的名称。
@Html.DropDownList( "Job.DefaultProfitCenter", (SelectList)ViewBag.DefaultProfitCenter, " -- Select -- ", new { id = "Job_DefaultProfitCenter" } )
This will generate the following html for the drop down list:
这将为下拉列表生成以下 html:
<select id="Job_DefaultProfitCenter" name="Job.DefaultProfitCenter">
<option value=""> -- Select -- </option>
<option value="0">Option 1</option>
</select>

