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..
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; }
}
The first argument in
@Html.DropDownList("MyIdAndName", (SelectList) ViewBag.list, " -- Select Business Type -- ")
is both the id and the name of theselect
tag.So,
would render
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.
This will generate the following html for the drop down list:
If you want to set the name and id or other attributes you can use the html attributes option.
NOTE: the method has a quirk: you can use the attributes dictionary to override the "id", but not the "name".
example:
will render:
So the upshot is: if you want name and id to be different, set the name parameter to what you want the name to be, and use the dictionary to override the id.