0

I want to give the user to option to add an item to a dropdownlist. I have tried implementing a couple of different solutions, but haven't been able to get the added item to appear in the list.

Currently, I am adding the item to the table and then recalling the HttpGet method to recreate the selectlist. The correct SelectList is created and sent to the View.

Controller:

Names= db.Contacts.ToList()

View:

@Html.DropDownListFor(m => m.Contact.ContactID,new SelectList(Model.Names.ToList(), "ContactID", "Name"), "")

The correct SelectList is being passed from the Controller to the View (i.e. the added item is included in the list) and when I step through the code, the View contains the correct list. However, the View is not being re-rendered.

Any help on this issue would be greatly appreciated!

3
  • Alternatively you want to populate Contact list server side as well right? Have you checked that at server side it is populating? Commented Sep 28, 2015 at 6:46
  • Yes, the list is updated server side, I've also checked that it is passed from Controller to View. Commented Sep 29, 2015 at 18:54
  • Have you checked it on View by debugging? Commented Sep 30, 2015 at 6:19

1 Answer 1

1

You could use JQuery to insert an item in your SelectList to avoid re-rendering the view. The logic will be then written in JavaScript, with an approach similar to the following:

Assuming your SelectList has the id selectList

$('#selectList').append('<option value=' + value +'>'+caption+'</option>');

Of course, this only takes care of the view, to be able to pass a new value, with its caption in the SelectList. it will not add the Name in the DB, server side.

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

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.