This is a migrated thread and some comments may be shown as answers.

[Solved] Append options to parent's DropDownList

1 Answer 128 Views
Window
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
R
Top achievements
Rank 1
R asked on 26 Sep 2011, 07:02 AM
I have a view with a dropdown list and a button to open a modal window where there is a text field and Save & Cancel buttons.
I'm loading the contents of the window from a partial. Within the partial, clicking Save does an ajax post and returns the saved index and name of the item. All the above works, but I can't append the newly saved item to the dropdownlist on the parent form - 'this.parent is null or not an object'.  The window close works fine, so is it that I'm just not getting the DropDownList correctly? Is there an alternate way to do this?



The View (the relevent bits):
...
        <div class="editor-field">
            @Html.DropDownListFor(model => model.SystemKeyID, new SelectList(ViewBag.SystemKeys, "SystemKeyID", "Name", Model.SystemKeyID), "--Select--")
            @Html.ValidationMessageFor(model => model.SystemKeyID)
            <button id="add-system-key">Add</button>
        </div>
...
@{ Html.Telerik().Window()
        .Name("Window")
        .Draggable(true)
        .Modal(true)
        .Buttons(b => b.Close())
        .Content("")
        .Width(300)
        .Height(80)
        .Visible(false)
        .Render();
}
...
@{ Html.Telerik().ScriptRegistrar()
        .OnDocumentReady(@<text>
            var windowElement = $('#Window');
            var addSystemKeyButton = $('#add-system-key');
            addSystemKeyButton
                .bind('click', function(e) {
                    OpenAddKeyWindow('Add Key');
                })
                .toggle(!windowElement.is(':visible'));
        </text>);
}
...
<script language="javascript" type="text/javascript">
    function OpenAddKeyWindow(title) {
        $("#Window .t-window-title").html(title);
        $.get('/CMS/CMSHTML/AddKeyPartial',
            function (data) {
                var window = $("#Window").data("tWindow");
                window.content(data);
                window.center();
                window.open();
            });
    }
</script>


The Partial:
@using (Html.BeginForm())
{
    <div id="content">
        <p><input id="TextAddKey" name="TextAddKey" type="text" /></p>
    </div>
    <div align='right'>
        <button type="button" onclick="saveWindow(event);">Save</button>
        <button type="button" onclick="closeWindow(event);">Cancel</button>
    </div>
<script type="text/javascript">
    function saveWindow(e) {
        $.post('/CMS/CMSHTML/CreateSystemKey', { TextAddKey: $('input[name=TextAddKey]').val() }, function (data) {
            if (data.Result == "success") {
                //Fails on next line
                this.parent.$('#SystemKeyID').append($('<option selected="selected"></option>').val(data.SystemKeyID).html(data.SystemKeyName));
            };
            this.parent.$("#Window").data("tWindow").close();
        }, "json")
    }
    function closeWindow(e) {
        this.parent.$("#Window").data("tWindow").close();
    }
</script>
}

1 Answer, 1 is accepted

Sort by
0
R
Top achievements
Rank 1
answered on 26 Sep 2011, 07:25 AM
Hmmm, this is odd...
Within the ajax post, this.parent does not seem to be necessary.

<script type="text/javascript">
    function saveWindow(e) {
        $.post('/CMS/CMSHTML/CreateSystemKey', { TextAddKey: $('input[name=TextAddKey]').val() }, function (data) {
            if (data.Result == "success") {
                $('#SystemKeyID').append($('<option selected="selected"></option>').val(data.SystemKeyID).html(data.SystemKeyName));
            };
            $("#Window").data("tWindow").close();
        }, "json")
    }
    function closeWindow(e) {
        this.parent.$("#Window").data("tWindow").close();
    }
</script>
The above works, but I'm not sure why the parent reference doesn't work in the post callback.
Tags
Window
Asked by
R
Top achievements
Rank 1
Answers by
R
Top achievements
Rank 1
Share this question
or