【发布时间】:2014-08-07 08:49:28
【问题描述】:
我是 ajax 新手,我正在尝试从类似的 ajax 方法调用 post 操作
$(".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");
}
});
});
我的 post 方法运行成功,但不是将我重定向到 MaSelection 视图,而是返回我调用该方法的第一个视图,所以我尝试在我的 ajax 方法中放置一个“成功”片段,然后我将位置替换为“马选择”视图,但我知道视图丢失了 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);
}
【问题讨论】:
-
你不能只传递 selectedId 和你要重定向到的目标 url 吗?
标签: c# javascript ajax asp.net-mvc-5