Add data using ajax to Modal asp.net mvc

I have a modal with bootstrap that allows me to add some data to the database using ajax ... after I load the data in a partial image and display it (classic)

this is my controller:

     public ActionResult AjouterCommentaire(TacheViewModel viewModel)
    {

        viewModel.NVcommentaire.DateCommentaire = DateTime.Now;
        viewModel.NVcommentaire.UtilisateurId = 4 ;

        db.Commentaires.Add(viewModel.NVcommentaire);
        db.SaveChanges();



        return PartialView("PartialCommentaire", db.Commentaires);
    }

      

problem: when I return a partial view PartialCommentaire

i.e. a table with data .. the table takes the entire modal space as the modal value of the table .. (and this is logical when I return this partial view)

if i go back: return Json(new { success = true });

it refreshes the page and i dont want that.

which i have to return, so the only thing to change is the partial view with the table

Think you are

Edit: (add html)

      @using (Ajax.BeginForm("AjouterCommentaire", new AjaxOptions { UpdateTargetId = "partialSummaryDiv" }))
      {
                <p>
                    <input type="submit" value="Create" />
                </p>
                        <div class="col-md-4">
                            @Html.TextAreaFor(model => model.NVcommentaire.TxtCommentaire, new { @class = " form-control", @placeholder = "Ajouter un Commentaire", @rows = "7" })
                        </div>
      }
                        <div class="col-md-8" id="partialSummaryDiv">
                            @{ Html.RenderPartial("PartialCommentaire", Model.Commentaires); }
                        </div>


            </div>

      

Edit2: I tried this, but it still doesn't work!

  <script>
$(document).ready(function () {
    $.ajax({
        url: "/Tache/AjouterCommentaire",
        type: "POST",
        success: function (result) {
            // refreshes partial view
            $('#partialSummaryDiv').html(result);
        }
    });

});

      

and changed the div like this:

   <div class="col-md-8" id="partialSummaryDiv">
     @{ Html.RenderPartial("PartialCommentaire", Model.Commentaires); }

      

+2


source to share


1 answer


Are you sure the Model.Commentaires object has the data before passing it to the second partial view?



0


source







All Articles