简体   繁体   中英

Add data using ajax in Modal asp.net mvc

i have a modal with bootstrap that allow me to add some data in database using ajax..after that i load data in partial view and show 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);
    }

the problem that when i return the partial view PartialCommentaire that is the table with data .. the table take all the modal space it's like the modal take the table value .. (and it's logic as i return that partial view)

if i return : return Json(new { success = true }); it refreshh the page, and i dont want that..

what i have to return so the only thing to change will be the partialview with the table

Think you

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 not working !!

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

});

and changed the div to be :

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

在将Model.Commentaires对象传递到第二部分视图之前,您确定它具有数据吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM