9

I tried for hours to get it work, but now I have to ask..

As read in
/942262/add-empty-value-to-a-dropdownlist-in-asp-net-mvc and /3780614/mvc-clear-default-selected-value-of-selectlist
I want to add an empty space to dropdown lists in my ASP.NET MVC Razor WebApp with Entity Framework 5 to force the user for an input.

I followed the informations, but what happend in the View is that: https://i.sstatic.net/EXSgT.png
So, as you see, there is an empty space, but it isn't selected by default. Instead, my "testproject", as first value from my controller, is marked.. Not the empty space. So where did I fail?

Thanks a lot.

My model looks like that:

    [...]
    using System.Web.Mvc;

    public class UploadTranferContainer
    {    
         [DisplayName("Project")]
         public int ProjectID { get; set; }

         public SelectList Projects { get; set; }
    }

My controller fills the data in like:

    UploadTranferContainer container = new UploadTranferContainer();
    container.Projects = new SelectList(db.Projects.AsEnumerable(), "ProjectID", "ProjectName");

And the view unpacks my container like:

        <div class="editor-label metadataLabel">
            @Html.LabelFor(x => x.ProjectID)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(x => x.ProjectID, Model.Projects, string.Empty)
        </div>            
3
  • possible duplicate of DropdownListFor default value Commented Dec 3, 2013 at 20:40
  • 2
    The selected value of the dropdown will be the value (if found) of Model.ProjectID in this case. If you want this to appear as the first (empty) option of the select, make sure the value of ProjectID is not contained in the select list items Model.Projects. Commented Dec 3, 2013 at 20:57
  • 1
    That should really be an answer, @Jay. Commented Dec 3, 2013 at 22:14

3 Answers 3

5

you can use below approach for dropdown with empty values

@Html.DropDownListFor(x => x.ProjectID, new SelectList(Model.Projects, "ProjectID", "ProjectName"), " ")
Sign up to request clarification or add additional context in comments.

Comments

4

The selected value of the dropdown will be the value (if found) of Model.ProjectID

If you want this to appear as the first (empty) option of the select, make sure the value of ProjectID is not contained in the select list items Model.Projects

Comments

0

Allows to create a empty list item

var area = new[] { new tbl_Area { AreaID = -1, AreaName = "Please Select Main Area" } };

or

ViewBag.SubAreaList = new[] { new tbl_Area { AreaID = -1, AreaName = "Please Select Main Area" } };

razor

@Html.DropDownList("DdlSubAreas", new SelectList(ViewBag.SubAreaList, "AreaID", "AreaName"), new { size = "10" });

tbl_Area can be a class or datamodel

class tbl_Area{
   public int AreaID {get;set;}
   public string AreaName {get;set;}
}

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.