更新后如何动态更新模态的内容?

时间:2019-01-28 13:35:24

标签: c# asp.net-mvc entity-framework bootstrap-modal

我有一个通过链接到数据库的数据表显示的消息列表。对于修改我的数据表的一行的操作,我使用模式,而当我修改我的数据表的一行时,数据库将更新为我的数据库,而不是我的模式。仅当刷新页面时,模态才会更新。修改完模态后是否可以动态更新模态?

我设法显示自己的模态,并使用ajax动态修改我的数据库和数据表。我试图通过刷新来修改模态的内容。

enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

@Html.BeginForm("Edit", "Nouvelle", null, FormMethod.Post, new { onsubmit = "return SubmitForm(this)" }))

<div class="form-group">

    @Html.LabelFor(model => model.message, "Titre", new { @class = "control-label" })

    @Html.EditorFor(model => model.titre, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.titre)
</div>

<div class="form-group">
    @Html.LabelFor(model => model.message, "Message", new { @class = "control-label" })
    @Html.TextAreaFor(model => model.message, new { @class = "form-control", @style = "height: 150px; max-width: 100%" })
    @Html.ValidationMessageFor(model => model.message)
</div>

<div class="form-group">
    @Html.LabelFor(model => model.auteur, "Auteur", new { @class = "control-label" })
    @Html.EditorFor(model => model.auteur, new { htmlAttributes = new { @class = "form-control", disabled = "disabled", @readonly = "readonly" } })
    @Html.ValidationMessageFor(model => model.auteur)
</div>

<div class="form-group">
    @Html.LabelFor(model => model.date_publication, "Dernière date de publication", new { @class = "control-label" })
    @Html.EditorFor(model => model.date_publication, new { htmlAttributes = new { @class = "form-control", disabled = "disabled", @readonly = "readonly" } })
    @Html.ValidationMessageFor(model => model.date_publication)
</div>

<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Retour</button>
    <button type="submit" class="btn btn-primary btn-ok">Modifier</button>
</div>

}

                                           ×                  修改                                                     
  var PopupForm = function (id) {
        var url = "/Nouvelle/EditId?id=" + id;

        $("#modificationContent").load(url, function () {
            $("#modification").modal("show");
        })
    }

    function SubmitForm(form) {
        $.ajax({
            type: "POST",
            url: "/Nouvelle/Edit",
            data: $(form).serialize(),
            success: function (response) {
                if (response.success) {
                    $("#modification").modal("hide");
                    dataTable.ajax.reload();
                    toastr.success("Votre news a bien été modifiée !");
                } else {
                    toastr.error("Les champs titre et/ou message(s) doive(nt) être rempli(s) !");
                }
            }
        });
        return false;
    }

[HttpPost]//controller
    public ActionResult Edit(News tempNews)
    {
        if (tempNews.titre != null && tempNews.message != null)
        {
            string test = tempNews.titre.Trim();
            if (tempNews.titre.Trim() != "" && tempNews.message.Trim() != "")
            {
                using (PortailEntities db = new PortailEntities())
                {
                    if (ModelState.IsValid && tempNews != null)
                    {
                        var v = db.News.Where(n => n.id_news == tempNews.id_news).FirstOrDefault();

                        v.titre = tempNews.titre.Trim();
                        v.message = tempNews.message.Trim();
                        v.auteur = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                        v.date_publication = DateTime.Now;
                    }
                    db.SaveChanges();

                    return Json(new { success = true }, JsonRequestBehavior.AllowGet);
                }
            }
        }
        return Json(new { success = false }, JsonRequestBehavior.AllowGet);
    }

我想动态更新模态作为数据表。

0 个答案:

没有答案