locked
load partial view after ajax post with post results RRS feed

  • Question

  • User-323149085 posted

    hello im trying to post data with ajax & load partialview with the posted item.

    how ever the partialView is loaded OK , but without the last posted item in it ,

    looking on debug i clear that the partialViw is loaded before save .

    here is some code:

    //View 
     <form method="post" data-ajax="true" data-ajax-failure="Failed" data-ajax-method="post" asp-controller="Request" asp-action="editQuestion" asp-antiforgery="true" id="form1">
    ///text field
    </form>
    //JS
    $(document).ready(function() {
        $(".BtnAskSeller").click(function() {
                   $(".modal").attr("id", "r-" + id);
            $(`#r-${id}`).modal("show");
            $("#form1").submit(function(e) {
                e.preventDefault();
                $.ajax(
                    {
                        type: 'POST',
                        data: {},
                        success: function() {
                            $(function() {
                                $('.success').fadeIn('fast');
                                $('.Faild').fadeOut('fast');
                                $(`#r-${OfferId}`).modal("hide");
                                document.getElementById("form1").reset();
                                $.ajax({
                                    url: '/Request/_QtoSelelr',
                                    dataType: 'html',
                                    contentType: 'application/html;charset:utf-8',
                                    type: 'GET',
                                    data: {
                                        OfferId: OfferId
                                    },
                                    success: function(data) {
                                                                            increaseQCoutByOne(OfferId);
                                        $('#form1')[0].reset();
                                        /* $(`#r-${OfferId}`).modal("hide");*/
                                    }
                                });
                            });
                        }
                    });
            });
        });
    });
    
    function increaseQCoutByOne(OfferId) {
        var numberQ = document.getElementById('Qcount-' + OfferId);
        var n1 = parseInt(numberQ.innerHTML.match(/\d+/g));
       /* var converttoint= parseInt(n1);*/
        numberQ.innerHTML = ' <i class="fas fa-question-circle"></i>' + " " + (n1 + 1) + " " + "MYTEXT HERE";
    }
    
    //controller
    //This action is to save posted item // Work Ok.
      [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<JsonResult> editQuestion(QToSeller model, int? id)
            {
                model.UserName = _userManager.GetUserName(User);
                try
                {
                    await _context.qToSellers.AddAsync(model);
                    await _context.SaveChangesAsync ();
                    return Json(" babby");
                }
                catch (Exception e)
                {
                    return Json(model, new {success = false, responseText = "Rrr"});
                }
            }

    //This is the partialView to load //THe Proble: Is loaded but not with the new item .

     public async Task<PartialViewResult> _QtoSelelr(int OfferId)
            {
                var questions = await _context.qToSellers.Where
                        (b => b.QuettaOfferId == OfferId)
                    .Include(c => c.SellerAns)
                    .ToListAsync ();
                return PartialView(questions);
            }

    Thanks

    Tuesday, May 25, 2021 10:55 AM

All replies

  • User475983607 posted

    The code you've shared does not make any logical sense.  Can you explain the design intent?  

    What is the purpose of the nested AJAX functions and why is the form configured for an AJAX request as well?  The outer AJAX function POSTs and empty model.  Why?   How or why is editQuestion called?

    Tuesday, May 25, 2021 11:10 AM
  • User-323149085 posted

    The code you've shared does not make any logical sense.  Can you explain the design intent?  

    the intent:

    on the page ( not the partial view ) user has a  list of items &  the ability to submit a form(boostrapModel) base on an id of the item ,after the form is submit , i need to load the partial view with the new item into a div in the page that is fit to the original  item (+- like comments to a  post on facebook) .

    thanks

    Tuesday, May 25, 2021 11:26 AM
  • User475983607 posted

    john_mm

    on the page ( not the partial view ) user has a  list of items &  the ability to submit a form(boostrapModel) base on an id of the item ,after the form is submit , i need to load the partial view with the new item into a div in the page that is fit to the original  item (+- like comments to a  post on facebook) .

    I don't use Facebook so I'm not sure you're trying to do but the code you've shared does not show how OfferId is populated. 

    data: {
        OfferId: OfferId
    },

    Or are you referring to  Id???  If so where does "id" get initialized???

    $(".modal").attr("id", "r-" + id);

    The code you've shared has a lot of issues which makes understanding the intent difficult.  What is the intent of the empty POST?  Why do you have nested AJAX calls?  What is the form configured as an AJAX form?

    At this point I recommend basic debugging and single stepping through the code.  Most likely your intension does not match what the code actual does.  If you single step through the code you'll be able to identify where the code stops working as you expect.

    Tuesday, May 25, 2021 12:26 PM
  • User-323149085 posted

    $(".modal").attr("id", "r-" + id);

    $(".modal").attr("id", "r-" + id);

    Im using some :( for example ) const id = $(this).attr("data-id"); in a few placesin order  to place values in my form, this is irrelevant to my question .

    Tuesday, May 25, 2021 1:34 PM
  • User475983607 posted

    Im using some :( for example ) const id = $(this).attr("data-id"); in a few placesin order  to place values in my form, this is irrelevant to my question .

    You did not answer any questions.  Why are you using a const?  Is there only one id?

    Tuesday, May 25, 2021 3:03 PM