How to create an empty dropdown list with default value?

asp.net-mvc, drop-down-menu

Solution

You may simply create an HTML Select option in your view.

<select id="parent" name="parent">
   <option value="">Select parent </option>
</select>

EDIT : As per the comment.

When you submit the form, You can get the selected value by either having a parameter with `parent` name

[HttpPost]
public ActionResult Create(string parent,string otherParameterName)
{
  //read and save and return / redirect
}

OR have a `parent` property in your ViewModel which you are using for Model binding.

public class CreateProject
{
  public string parent { set;get;}
  public string ProjectName { set;get;}
}

and in your action method.

[HttpPost]
public ActionResult Create(CreateProject model)
{

  // check model.parent value.
}

Problem

I want to create an empty dropdown list that has just default value.I use the following code: ``` @Html.DropDownList("parent","--Select Parent--") ``` But in running time I see this error: There is no ViewData item of type 'IEnumerable' that has the key 'parent'. How can I solve it? Thanks.

Original source

Related problems