Note that there are some explanatory texts on larger screens.

plurals
  1. POForm default value not wanted after ajax submission
    primarykey
    data
    text
    <p>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.</p> <p>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)</p> <p>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</p> <p>document.getElementById("editForm").reset();</p> <p>with no luck.</p> <p>Thanks</p> <p>The script</p> <pre><code>(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)); </code></pre> <p>And the view in case is relevant</p> <pre><code>@model ContactListViewModel @{ ViewBag.Title = " My Contacts"; } &lt;div id="myContacts"&gt; &lt;h2&gt;My Contacts&lt;/h2&gt; &lt;div id="editContainer" data-amp-edit-url="@Url.Action("Edit", "Contacts")" class="initiallyHidden"&gt;&lt;/div&gt; &lt;div id="list"&gt; @{ Html.RenderPartial("_ContactList", Model); } &lt;/div&gt; &lt;div id="dialog" data-amp-del-url="@Url.Action("Delete", "Contacts")" title="Confirmation Required"&gt; &lt;p&gt;Are you sure you want to delete this Contact?&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; @section Foot { &lt;script src="~/Scripts/AMPContacts.js"&gt;&lt;/script&gt; &lt;script src="~/Scripts/conditional-validation.js"&gt;&lt;/script&gt; &lt;script src="~/Scripts/Placeholders.min.js"&gt;&lt;/script&gt; } </code></pre> <p>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</p> <pre><code>@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 =&gt; m.Id) @Html.HiddenFor(m =&gt; m.OwnedItemId) @Html.HiddenFor(m =&gt; m.AddressId) &lt;div id="editContactDetails"&gt; &lt;div&gt; @Html.DisplayFor(m =&gt; m.PlanName) &lt;/div&gt; &lt;div&gt; @Html.DropDownListFor(m =&gt; m.Title, Model.TitleList, "Title") &lt;/div&gt; &lt;div&gt; @Html.EditorFor(m =&gt; m.FirstName, new { @id="editFirstName", data_placeholders_focus = "false", placeholder = ViewData.ModelMetadata.Watermark }) @Html.ValidationMessageFor(m =&gt; m.FirstName) &lt;/div&gt; // And so on.... &lt;div class="addDEC"&gt; &lt;input class="addDECButton" type="submit" value="Save" /&gt; &lt;/div&gt; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload