简体   繁体   English

在Modal ASP.NET MVC中使用Ajax添加数据

[英]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) 我有一个带引导程序的模态,允许我使用ajax在数据库中添加一些数据。之后,我在部分视图中加载数据并显示它(经典)

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) 问题是,当我返回带有数据的表的部分视图PartialCommentaire ,该表占据了所有模态空间,就像模态占据了表值..(这是逻辑,因为我返回了该部分视图)

if i return : return Json(new { success = true }); 如果我返回: 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 我必须返回的内容,所以唯一要更改的是与表格的partialview

Think you 想你

Edit : (add html ) 编辑:(添加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 !! Edit2:我试过了,但是还是不能用!

  <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更改为:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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