1

I have this script, what it intends to do is: in a view with a list of contacts, each one with a edit link whenever you click on one of these links it displays an edit form in a partial view (there's only one div in the parent view for holding it) just below the clicked contact, for that it does a get ajax call to the server and gets the information for that contact. After submitting, via ajax as well, it refreshes the list.

Everything seems to work fine, but if I edit a contact, after the list being refreshed I try to open again the form on the same contact and the information is the same as it was before being edited. Although debugging I see the list for the server is correct (and so is the display in the list, it's only wrong in the form)

Thing is that in chrome developer tool I can see the form with some "default values" set to the previous stuff. I didn't know about them till now and I don't know how to get rid of them, because to my understanding I'm making a get call to the server, anyway I have tried this

document.getElementById("editForm").reset();

with no luck.

Thanks

The script

(function ($) {
    var editContainer = $(document.getElementById('editContainer'));

    $('#editContainer').on('submit', '#editForm', ajaxEditCall);

    $('a.edit').on("click", displayEditForm);

    function displayEditForm(e) {
        var clickedElement = $(this).parent(),

        url = editContainer.data('amp-edit-url');

        $.get(url, {
            id: parseInt(this.id, 10)
        }, function (result) {
            editContainer.html(result);
            $.validator.unobtrusive.parse(editContainer);
            // Display edit form just below the "item" clicked
            if (editContainer.is(":visible")) {
                editContainer.slideToggle(300, function () {
                    editContainer.appendTo(clickedElement);
                    editContainer.slideToggle(300);
                });
            } else {
                editContainer.appendTo(clickedElement);
                editContainer.slideToggle(300);
            }
        }, "html");
        e.preventDefault();
    }

    function ajaxEditCall(e) {
        e.preventDefault();
        //if ($('#editForm').valid()) {
        var list = $(document.getElementById('list'));

        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                if (result.passedValidation == true) {
                    $.get(result.action, function (partial) {
                        document.getElementById("editForm").reset();
                        list.html(partial);
                        $('a.edit').on("click", displayEditForm);
                        $('#editForm').slideUp(300);
                        setTimeout(function () {
                            list.effect("highlight", {}, 3000);
                        }, 1000);
                    });
                } else {
                    $(document).scrollTop(0);
                    editContainer.html(result);
                    $.validator.unobtrusive.parse('#editForm');
                }
            }
        });
        //} return false;
    }
}(jQuery));

And the view in case is relevant

@model ContactListViewModel

@{
    ViewBag.Title = " My Contacts";
}

<div id="myContacts">
    <h2>My Contacts</h2>

    <div id="editContainer" data-amp-edit-url="@Url.Action("Edit", "Contacts")" class="initiallyHidden"></div>

    <div id="list">
        @{ Html.RenderPartial("_ContactList", Model); }
    </div>
    <div id="dialog" data-amp-del-url="@Url.Action("Delete", "Contacts")" title="Confirmation Required">
        <p>Are you sure you want to delete this Contact?</p>
    </div>
</div>

@section Foot
{
    <script src="~/Scripts/AMPContacts.js"></script>
    <script src="~/Scripts/conditional-validation.js"></script>
    <script src="~/Scripts/Placeholders.min.js"></script>
}

Well that's obviously the parent view, the partial one is just a bunch of fields, I remove most of them to avoid increase the already long post

@model AddContactViewModel

@using (Html.BeginForm("Edit", "Contacts", FormMethod.Post, new { @id = "editForm", @class = "addTab" }))
{
    @Html.ValidationSummary(true, "Please correct the errors and try again.")

    @Html.HiddenFor(m => m.Id)
    @Html.HiddenFor(m => m.OwnedItemId)
    @Html.HiddenFor(m => m.AddressId)

    <div id="editContactDetails">
            <div>
                @Html.DisplayFor(m => m.PlanName)
            </div>

            <div>
                @Html.DropDownListFor(m => m.Title, Model.TitleList, "Title")
            </div>
            <div>
                @Html.EditorFor(m => m.FirstName, new { @id="editFirstName", data_placeholders_focus = "false", placeholder = ViewData.ModelMetadata.Watermark })
                @Html.ValidationMessageFor(m => m.FirstName)
            </div>

        // And so on....

        <div class="addDEC">
            <input class="addDECButton" type="submit" value="Save" />
        </div>
}
4

1 回答 1

1

我在一个正在开发的网站上遇到了这个问题。您需要禁用 ajax 缓存:

//Disbable cache for all jQuery AJAX requests
$.ajaxSetup({ cache: false });

这将对所有 ajax 调用执行此操作,因此您可能只想在某些页面上执行此操作,具体取决于您的使用情况。

于 2013-05-06T22:14:22.757 回答