我有一个MVC 3项目,您可以在其中编辑记录或克隆它。当您单击要编辑的记录时,该网址如下所示:
http://localhost/Products/Edit/?Id=123
如果用户单击克隆,则会出现一个弹出窗口,询问是否有新名称。用户输入名称然后单击继续。使用旧的id和新名称进行ajax调用:
var formData = "Id="+@Model.Id+"&Name="+$('#txt').val();
$.ajax({
url: '@Url.Action("Clone")',
type: "POST",
data: formData,
success: function(resp) {
//this is where i try to refresh the page with the newly created id
window.location.href = "@(Url.Action("Edit") + "/?Id=" + resp.NewId)";
},
error:function(data) {
},
complete:function() {
}
});
这是我的行动:
public ActionResult Copy(Inputmodel viewModel)
{
//this will populate the new id inside of my model object
var model = Clone(viewModel);
return View("Edit", model);
}
现在我需要使用新创建的ID重新加载浏览器,因此网址应如下所示:
http://localhost/Products/Edit/?Id=456
我在我的ajax调用中尝试了上述内容,但这并没有奏效。我如何实现我想要做的事情?谢谢
答案 0 :(得分:2)
你没有正确连接。
这样做:
window.location.href = "@Url.Action("Edit")"+"?Id=" + resp.NewId;
或者:
window.location.href = "@Url.Action("Edit")?Id=" + resp.NewId;
答案 1 :(得分:1)
您正在寻找RedirectToAction。
尝试更换:
return View("Edit", model);
与
return RedirectToAction("Edit","Products",new {Id="456"}/*set query string*/);