0

Prior to MVC 5 the DropDownList was scaffolded in my Razor Edit View as:

@Html.DropDownList("EntityId", String.Empty)

But now the equivalent (post MVC 5) is scaffolded as:

@Html.DropDownList("EntityId", null, htmlAttributes: new { @class = "form-control" })

I want to add the htmlAttributes to keep the UI consistent, but the DropDownList no longer works as expected for a nullable EntityId (int ? EntityId). The problem is that the default item is set to the first item in the list if EntityId is null. I want a blank item if the id is null, and the appropriate selection if not.

PS. My controller builds the list as follows: ViewBag.EntityId = new SelectList(db.Entities, "EntityId", "Name", EntityId);

3
  • Does your model have a property your binding to? (you should not have the same name for the model property and the ViewBag SelectList property) Commented Dec 16, 2014 at 5:01
  • 1
    Yes it does, could that be causing the problem? Not sure why I ever did this, but I suspect it is like that in a lot of my code. I have just seen your reply below, and will try this out back at work tomorrow. Thx Commented Dec 16, 2014 at 8:58
  • Just noticed why I use the same property name for both the model and ViewBag - it is because it is scaffolded that way in the controller by default - something to be aware of. Commented Jan 5, 2015 at 21:41

2 Answers 2

4

Use the strongly typed helpers. If your model has a property int? EntityId, then in the controller

ViewBag.EntityList = new SelectList(db.Entities, "EntityId", "Name")

Note the ViewBag property should not be the same name as the model property, and the 3rd parameter of 'SelectList` is not required.

Then in the view

@Html.DropDownListFor(m => m.EntityId, (SelectList)ViewBag.EntityList, "--Please select--", new  { @class = "form-control" })

If EntityId is null, the first option will be selected, otherwise if EntityId matches the value of one of your options then it will be selected.

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

4 Comments

Do you mean ViewBag.EntityList not Model.EntityList?
Yes I do :) will update in a momement (I always use view models and hate ViewBag)
That did it thanks - the name of the ViewBag property didn't seem to make any difference though. Is there a specific reason why the ViewBag property and model property names can't be the same? Just curious.
I was wrong - if the ViewBag property name is the same as the Model property name, then the selected item is not selected, and this was the source of my problems from the outset.
1

Modify your @HTML.DropDownList to

@Html.DropDownList("EntityId", (SelectList)Viewbag.EntityList,String.Empty, new { @class = "form-control" })`

1 Comment

This worked too thanks, once I added a cast for the SelectList (edited your post). Thanks.

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.