add new option to dropdownlist after jquery dialog and post

Posted by RememberME on Stack Overflow See other posts from Stack Overflow or by RememberME
Published on 2010-04-21T20:40:37Z Indexed on 2010/04/21 20:43 UTC
Read the original article Hit count: 429

I have a form to enter subcontracts. On this form I have a dropdownlist of all companies in the system. Next to it is a button "Create Company". This button opens a jquery dialog which allows the user to create a new company. Once the dialog closes, the new company needs to be added to the dropdownlist and selected. If I refresh, it's there, but I need to do it without refreshing the form b/c I don't want the user to loose everything that they've entered into the other fields. I'm not sure how to do this b/c I don't have the guid of the new company.

My jquery dialog:

 $('#popupCreateCompany').dialog(
                {
                    autoOpen: false,
                    modal: true,
                    buttons:
                    {
                        'Add': function() {
                            var dialog = $(this);
                            var form = dialog.find('input:text, select');
                            $.post('/company/create', $(form).serialize(), function() {
                                dialog.dialog('close');
                            })
                        },
                        'Cancel': function() {
                            $(this).dialog('close');
                        }
                    }
                });


        $("#create-company").click(function() {
            $('#popupCreateCompany').dialog('open');
        });

Company field:

<label for="company">Company:</label>
            <%= Html.DropDownList("company", Model.SelectCompanies, "** Select Company **") %>
            <%= Html.ValidationMessage("Company", "*") %>
            <button type="button" id="create-company" >Create Company</button>

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about jQuery