【发布时间】:2016-07-08 01:46:01
【问题描述】:
我有一个带有引导程序的模式,允许我使用 ajax 在数据库中添加一些数据..之后我在部分视图中加载数据并显示它(经典)
这是我的控制器:
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);
}
当我返回部分视图时的问题PartialCommentaire
那是带有数据的表格..表格占用所有模态空间,就像模态占用表格值..(这是我返回部分视图时的逻辑)
如果我返回:return Json(new { success = true }); 它会刷新页面,我不希望这样..
我必须返回,所以唯一需要改变的是带有表格的部分视图
想你
编辑:(添加 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>
编辑2: 我试过了,但还是不行!!
<script>
$(document).ready(function () {
$.ajax({
url: "/Tache/AjouterCommentaire",
type: "POST",
success: function (result) {
// refreshes partial view
$('#partialSummaryDiv').html(result);
}
});
});
并将 div 更改为:
<div class="col-md-8" id="partialSummaryDiv">
@{ Html.RenderPartial("PartialCommentaire", Model.Commentaires); }
【问题讨论】:
-
您必须提供执行 ajax 发布和 dom 替换的 javascript 代码以及模态的 html。
-
@MenelaosVergis 已编辑:(ps:我没有使用任何 javascript,我只是使用 ajax.beginforms,如果它们不是 Modal,它就可以工作。问题在于将数据返回到模型
标签: javascript c# jquery asp.net-mvc twitter-bootstrap