Reputation: 1237
I want to inserd new record into my entity Table.
While binding the records to Dropdownlist, we add one new ListItem like "- Select One -".
If it is using ADO.NET Datasets, it is much simpler as below. But now i am using Entity Model.
Ex using ADO.NET:
ListItemCollection var_NewLists = new ListItemCollection();
ListItem dummyItem = new ListItem();
dummyItem.Value = "";
dummyItem.Text = "- Select One -";
var_NewLists.Add(dummyItem);
foreach (DataRow theRow in ds.Tables[0].Rows)
{
ListItem newItem = new ListItem();
newItem.Value = theRow[P_Id].ToString();
newItem.Text = theRow[Name].ToString();
var_NewLists.Add(newItem);
}
Now i want to do the same using Entity Model and i want to insert new record into this list before i bind it to the Dropdown.
Here is my Product table from Entity.
var product =
(from p in miEntity.Product
where p.Pipeline == false
orderby p.Name
select new
{
P_Id = p.P_Id,
P_Name = p.Name
}).ToList();
Upvotes: 0
Views: 739
Reputation: 31842
You can do it in View:
<%= Html.DropDownList("Product", new SelectList(Model.Products, "ID", "Name"), "Select one") %>
http://msdn.microsoft.com/en-us/library/dd504970.aspx
optionLabel
- Type: System.String
- The text for a default empty item. This parameter can be null reference (Nothing in Visual Basic).
Upvotes: 2