Javascript 在模式asp.net mvc中使用ajax添加数据
我有一个引导模式,允许我使用ajax在数据库中添加一些数据。之后,我在部分视图中加载数据并显示它(经典) 这是我的控制器:Javascript 在模式asp.net mvc中使用ajax添加数据,javascript,c#,jquery,asp.net-mvc,twitter-bootstrap,Javascript,C#,Jquery,Asp.net Mvc,Twitter Bootstrap,我有一个引导模式,允许我使用ajax在数据库中添加一些数据。之后,我在部分视图中加载数据并显示它(经典) 这是我的控制器: public ActionResult AjouterCommentaire(TacheViewModel viewModel) { viewModel.NVcommentaire.DateCommentaire = DateTime.Now; viewModel.NVcommentaire.UtilisateurId =
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);
}
返回部分视图时出现的问题partialcommentair
这是包含数据的表。。表格占据所有模态空间,就像模态占据表格值一样。。(这是我返回部分视图时的逻辑)
如果我返回:returnjson(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>
@使用(Ajax.beginnform(“ajutercommentaire”,新的AjaxOptions{UpdateTargetId=“partialSummaryDiv”}))
{
@Html.TextAreaFor(model=>model.nvecommentaire.txtccommentaire,新的{@class=“form control”,@placeholder=“ajourn Commentaire”,@rows=“7”})
}
@{Html.RenderPartial(“PartialCommentaire”,Model.commentaries);}
Edit2:
我试过了,但还是不行
<script>
$(document).ready(function () {
$.ajax({
url: "/Tache/AjouterCommentaire",
type: "POST",
success: function (result) {
// refreshes partial view
$('#partialSummaryDiv').html(result);
}
});
});
$(文档).ready(函数(){
$.ajax({
url:“/Tache/ajutercommentaire”,
类型:“POST”,
成功:功能(结果){
//刷新局部视图
$('#partialSummaryDiv').html(结果);
}
});
});
并将div更改为:
<div class="col-md-8" id="partialSummaryDiv">
@{ Html.RenderPartial("PartialCommentaire", Model.Commentaires); }
@{Html.RenderPartial(“PartialCommentaire”,Model.commentaries);}
在将模型.Commentaries对象传递到第二个局部视图之前,您确定该对象具有数据吗?您必须提供执行ajax post和dom替换的javascript代码,以及要开始的模式html。@MenelaosVergis编辑:(注:我没有使用任何javascript,我只使用ajax.beginforms,如果它们没有模式,它就可以工作。)问题在于将数据返回到模型中。问题不在模型中,因为所有数据都会毫无问题地传递……问题是当控制器操作返回部分视图时……解决方法是更改返回类型(但我不知道返回什么)你能在这里放一些代码吗?部分视图的操作应该返回PartialViewResultyes部分视图的操作返回partialView..但问题是部分视图替换了模式中的原始视图(因此表占据了模式中的所有位置)删除RenderPartial,用一个空div替换它,然后向controller操作发布一个ajax请求,并将div的内容设置为从部分视图返回的HTML,怎么样?老实说,我没有得到它。我看起来也一样,因为RenderPartial返回HTML(我知道!)