views:

253

answers:

1

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/company_id 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>

SelectList declaration:

SelectCompanies = new SelectList(subcontractRepository.GetPrimaryCompanies(), "company_id", "company_name", Subcontract.company);
A: 

I got it. I returned a Json with the guid.

            $('#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(data) {
                                alert(data.Result);
                                if (data.Result == "success") {

                                    $('#company').append($('<option selected="selected"></option>').val(data.company_id).html(data.company_name));
                                };
                                dialog.dialog('close');
                            }, "json")
                        },
                        'Cancel': function() {
                            $(this).dialog('close');
                        }
                    }
                });
RememberME