添加用ajax数据莫代尔asp.net的MVC [英] Add data using ajax in Modal asp.net mvc

查看:129
本文介绍了添加用ajax数据莫代尔asp.net的MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个引导模式,允许我使用ajax..after我加载局部视图数据在数据库中添加一些数据并显示它(经典)

这是我的控制器:

 公众的ActionResult AjouterCommentaire(TacheViewModel视图模型)
    {        viewModel.NVcommentaire.DateCommentaire = DateTime.Now;
        viewModel.NVcommentaire.UtilisateurId = 4;        db.Commentaires.Add(viewModel.NVcommentaire);
        db.SaveChanges();        返回PartialView(PartialCommentaire,db.Commentaires);
    }

问题,当我返回局部视图 PartialCommentaire
这是与数据的表..表把所有的模态空间就好像模​​态取表值。(和它的逻辑,我返回局部视图)

如果我回:返回JSON(新{成功=真}); 它refreshh页,我不想说。

我要回到这样改变的唯一的事情将与表partialview

你觉得你

编辑:(添加HTML)

  @using(Ajax.BeginForm(AjouterCommentaire,新AjaxOptions {UpdateTargetId =partialSummaryDiv}))
      {
                &所述p为H.;
                    <输入类型=提交值=创建/>
                &所述; / P>
                        < D​​IV CLASS =COL-MD-4>
                            @ Html.TextAreaFor(型号=> model.NVcommentaire.TxtCommentaire,新{@class =表格控,@placeholder =Ajouter联合国评注,@rows =7})
                        < / DIV>
      }
                        < D​​IV CLASS =COL-MD-8ID =partialSummaryDiv>
                            @ {Html.RenderPartial(PartialCommentaire,Model.Commentaires); }
                        < / DIV>
            < / DIV>

EDIT2:
我试过,但它仍然没有工作!

 <脚本>
$(文件)。就绪(函数(){
    $阿贾克斯({
        网址:/环节存在/ AjouterCommentaire
        键入:POST,
        成功:函数(结果){
            //刷新局部视图
            $('#partialSummaryDiv')HTML(结果)。
        }
    });});

和改变了股利是:

 < D​​IV CLASS =COL-MD-8ID =partialSummaryDiv>
     @ {Html.RenderPartial(PartialCommentaire,Model.Commentaires); }


解决方案

您确定Model.Commentaires对象有数据,你把它传递给第二局部视图之前?

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); }

解决方案

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

这篇关于添加用ajax数据莫代尔asp.net的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆