How to refresh div after AJAX request in MVC?

DiPix

I'm not sure how to refresh data after I use AJAX. Here's what I already have:

Frontend:

@model TFU.Model.DB_USER

    <div id="listTelNumbers">
        @foreach (var item in Model.DB_USER_PHONES)
        {
            <dl class="dl-horizontal">
                <dt>
                    @item.PHONE
                </dt>
                <dd>
                    <button id="removeButton" class="btn btn-default" onclick="sendRequestToRemove('@item.USER_ID', '@item.PHONE')">Usuń</button>
                </dd>
            </dl>
        }
    </div>

Script - fadeOut works fine but I don't know what should I fadeIn. So I guess is missing a small part of code there. Also how can I fadeOut only the record which I currently removing instead all list.

 <script>
        function sendRequestToRemove(id, phone) {
            var data = {
                "USER_ID": id,
                "PHONE": phone
            }


            $.ajax({
                url: '/User/RemoveTelNumber',
                data: JSON.stringify(data),
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                error: function (err) {
                    alert('Error: ' + err.statusText);
                },
                success: function (result) {
                    $('#listTelNumbers').fadeOut(800, function () {
                        form.html('#listTelNumbers').fadeIn().delay(2000);
                    });
                },
                async: true,
                processData: false
            });
        }      
    </script>

Backend:

public bool RemoveTelNumber(DB_USER_PHONES model)
{
    var user = db.DB_USER_PHONES.First(x => x.USER_ID == model.USER_ID && x.PHONE == model.PHONE);
    db.DB_USER_PHONES.Remove(user);
    db.SaveChanges();
    return true;
}
user3559349

Firstly, your loop is generating duplicating invalid html because of the duplicate id attributes. And you should not be polluting your markup with behavior - use Unobtrusive JavaScript. Change the html to remove the id attribute, add a class name for selection and add data-* attributes for the values to be posted

@foreach (var item in Model.DB_USER_PHONES)
{
    <dl class="dl-horizontal">
        <dt>@item.PHONE</dt>
        <dd><button class="btn btn-default delete" data-id="@item.USER_ID" data-phone="@item.PHONE">Usuń</button></dd>
    </dl>
}

Then change the script to

var url = '@Url.Action("RemoveTelNumber", "User")'; // always use Url.Action()
$('.delete').click(function() {
    var container = $(this).closest('dl');
    var data = { user_Id: $(this).data('id'), phone: $(this).data('phone') };
    $.post(url, data, function(response) {
        if (response) {
            // fadeout, then remove
            container.fadeOut(800, function() { $(this).remove(); })
        } else {
            // Oops - display an error message?
        }
    }).fail(function() {
        // Oops
    });
});

And finally, change the action method to return a JsonResult indicating success or otherwise

[HttpPost]
public JsonResult RemoveTelNumber(DB_USER_PHONES model)
{
    var user = db.DB_USER_PHONES.First(x => x.USER_ID == model.USER_ID && x.PHONE == model.PHONE);
    db.DB_USER_PHONES.Remove(user);
    db.SaveChanges();
    return Json(true);
    // or if something went wrong, return Json(null);
}

I also recommend you rename you classes and properties to follow conventional naming practices - UserPhone, not DB_USER_PHONES, UserId, not USER_ID etc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MVC- After ajax request Page cannot be refresh

From Dev

Ajax refresh in mvc div data

From Dev

Continue AJAX request after page refresh

From Dev

Refresh PHP SESSION var after AJAX request

From Dev

refresh data in view after ajax request symfony

From Dev

How to refresh div using ajax refresh?

From Dev

reload div after ajax request

From Dev

Ajax refresh div after all data is loaded

From Dev

How to Stop MVC Page Refresh After Ajax Call url Change when Keyboard Enter Pressed

From Dev

How to refresh a model after a request to API

From Dev

How to refresh HTML after AJAX post

From Dev

Unwanted page refresh after AJAX request that runs SQL

From Dev

Response back to div after ajax request

From Dev

Refresh specific div after AJAX UPDATER with prototype js

From Dev

Refresh Div's content after AJAX form submission

From Dev

Ajax refresh div is showing old data after refreshing

From Dev

AJAX Refresh Div Not Functioning

From Dev

Refresh a DIV on ajax success

From Dev

How to refresh a BeanFieldGroup after a client request whitout a button?

From Dev

ajax request, but refresh the whole page?

From Dev

Ajax request after MVC authentication timeout/session cookie deleted

From Dev

Ajax request after MVC authentication timeout/session cookie deleted

From Dev

Fill in Bootstrap table after Ajax request (JSP and Spring MVC)

From Dev

React - How to Update State After AJAX Request

From Dev

How to transition routes after an ajax request in redux

From Dev

How to delete file after access it with ajax request?

From Dev

how to stop ajax request after type:Post

From Dev

How to refresh or reload a div's content using Ajax

From Dev

How to use AJAX for auto refresh a div or a web page

Related Related

  1. 1

    MVC- After ajax request Page cannot be refresh

  2. 2

    Ajax refresh in mvc div data

  3. 3

    Continue AJAX request after page refresh

  4. 4

    Refresh PHP SESSION var after AJAX request

  5. 5

    refresh data in view after ajax request symfony

  6. 6

    How to refresh div using ajax refresh?

  7. 7

    reload div after ajax request

  8. 8

    Ajax refresh div after all data is loaded

  9. 9

    How to Stop MVC Page Refresh After Ajax Call url Change when Keyboard Enter Pressed

  10. 10

    How to refresh a model after a request to API

  11. 11

    How to refresh HTML after AJAX post

  12. 12

    Unwanted page refresh after AJAX request that runs SQL

  13. 13

    Response back to div after ajax request

  14. 14

    Refresh specific div after AJAX UPDATER with prototype js

  15. 15

    Refresh Div's content after AJAX form submission

  16. 16

    Ajax refresh div is showing old data after refreshing

  17. 17

    AJAX Refresh Div Not Functioning

  18. 18

    Refresh a DIV on ajax success

  19. 19

    How to refresh a BeanFieldGroup after a client request whitout a button?

  20. 20

    ajax request, but refresh the whole page?

  21. 21

    Ajax request after MVC authentication timeout/session cookie deleted

  22. 22

    Ajax request after MVC authentication timeout/session cookie deleted

  23. 23

    Fill in Bootstrap table after Ajax request (JSP and Spring MVC)

  24. 24

    React - How to Update State After AJAX Request

  25. 25

    How to transition routes after an ajax request in redux

  26. 26

    How to delete file after access it with ajax request?

  27. 27

    how to stop ajax request after type:Post

  28. 28

    How to refresh or reload a div's content using Ajax

  29. 29

    How to use AJAX for auto refresh a div or a web page

HotTag

Archive