10

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.

4
  • Try this @Html.DropDownList(null, "--Select Parent--"). Commented Mar 26, 2013 at 15:04
  • @MichaelPerrenoud : And now the error is changed to this:Value cannot be null or empty. Parameter name: name Commented Mar 26, 2013 at 15:11
  • OK, then that won't work. You'll need to follow @Shyju direction. The issue is it's trying to bind to a property named "parent". Commented Mar 26, 2013 at 15:12
  • you can simply create an HTML select option. but the question is still alive, why you want to create an empty dropdown? the good way is to create one page for NEW and EDIT and use a ViewModel to generate data. Commented Sep 18, 2019 at 7:38

4 Answers 4

26

You can build an empty DropDownList like this:

@Html.DropDownList("parent", Enumerable.Empty<SelectListItem>(), "--Select Parent--")

Reference: Build an empty MVC DropdownListFor for a Cascade Sub-List

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You your partially helped me to return empty dropdown .. My Code : IW.SelItemList = new SelectList(Enumerable.Empty<SelectListItem>());
9

Adding html attributes to the above example:

    @Html.DropDownList("Idparent", Enumerable.Empty<SelectListItem>(), "Select one...", new {@class="form-control"})                 

1 Comment

From review queue: May I request you to please add some context around your source-code. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post.
6

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.
}

3 Comments

But this kind of HTML objects would not be submited in the forms like this: @using (Html.BeginForm()) {}
ofcourse it will. Whatever Helper method you use (ex : Html.DropDownListFor) will also generate HTML markup like this only. In your HttpPost action, you can use a parent parameter or have a parent property in your viewmodel.
Just the name of my dropdown list should be the same with the property in my view model.there is no need to get it in my controller.In the controller I just need to get a variable with type of view model.Art of MVC...
0

You can do something like this in your controller to build a empty dropdown list

    ViewBag.ClassID = new SelectList(db.Classes.Where(c => c.ClassID == 0)  , "ClassID", "ClassName").ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.