我是ajax的新手,我试图通过像这样的ajax方法调用后期操作
$(".buttonSelection").click(function () {
selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value');
$.ajax({
// Call MaSelection action method
url: "/DemandeLocation/MaSelectionOffre",
data: { id: selectedId },
type: 'Post',
success: function (msg) {
window.location.replace('@Url.Content("~/DemandeLocation/MaSelectionOffre")');
},
error: function (xhr) {
alert("something seems wrong");
}
});
});
我的帖子方法取得了成功,但不是将我重定向到MaSelection View,而是返回我调用方法的第一个视图,所以我尝试了一个" Success"我的ajax方法中的片段和我选择的位置替换为" Ma选择"查看,但我知道视图丢失了ID,因此它变为null,我怎么能用Ajax,
此处我的帖子操作了解更多详情
[HttpPost]
[Authorize(Roles = "Locataire")]
public ActionResult MaSelectionOffre(string id)
{
int DemandeLocationGetbyId = Convert.ToInt32(id);
var selectionOffre = db.SelectionOffreLocationSet.Where(model => model.DemandeLocationPublication_ID == DemandeLocationGetbyId).ToList();
return View("MaSelectionOffre", selectionOffre);
}
答案 0 :(得分:1)
使用json
作为数据类型;
$(".buttonSelection").click(function () {
selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value');
$.ajax({
// Call MaSelection action method
url: "/DemandeLocation/MaSelectionOffre",
dataType:"json",
data: { id: selectedId },
type: 'Post',
success: function (msg) {
window.location.href = msg.redirect;
},
error: function (xhr) {
alert("something seems wrong");
}
});
});
你也需要这个;
答案 1 :(得分:1)
如果你想要重定向页面,在ajax调用之后你应该使用
...
success: function (msg) {
window.location.href = '@Url.Action("MaSelectionOffre", "DemandeLocation")';
},
...
修改强>
如果您想要替换结果,请使用以下内容:
<强> HTML 强>
<div id="updateTargetId">
//table
//tr
//td
//your button that has cssClass buttonSelection
</div>
<强> JS 强>
$(".buttonSelection").click(function () {
selectedId = $(this).parents('tr:first').children('td:first').children('input:first').attr('value');
$.ajax({
// Call MaSelection action method
url: "/DemandeLocation/MaSelectionOffre",
dataType:"json",
data: { id: selectedId },
type: 'Post',
success: function (msg) {
$("#updateTargetId").html(msg);
},
error: function (xhr) {
alert("something seems wrong");
}
});
});
CONTROLLER (返回PartialView)
[HttpPost]
[Authorize(Roles = "Locataire")]
public ActionResult MaSelectionOffre(string id)
{
int DemandeLocationGetbyId = Convert.ToInt32(id);
var selectionOffre = db.SelectionOffreLocationSet.Where(model => model.DemandeLocationPublication_ID == DemandeLocationGetbyId).ToList();
return PartialView("MaSelectionOffre", selectionOffre);
}
答案 2 :(得分:0)
我将动作改为get动作,在我的按钮中,我刚添加了带链接和ID的window.location.replace
<button type="button" class="buttonSelection" onclick="window.location.replace('@Url.Content("~/DemandeLocation/MaSelectionOffre?id="+item.Publication_ID)')"> <span class="ui-icon ui-icon-cart"></span> </button>