【发布时间】:2012-04-24 07:13:01
【问题描述】:
如何在调用重定向(或等效方法)的控制器方法中刷新视图?
查看(Index.cshtml):
@model ViewModel
@Html.EditorFor(model => model.First)
@Html.EditorFor(model => model.Last)
@Html.EditorFor(model => model.Junk)
// call is invoked to PerformMagic as such:
onComplete: function (event, queueID, fileObj, response, data)
{
$.ajax(
{ url: '@Url.Action("PerformMagic","Home")',
data: { first: $("#fname").val() },
})
}
Junk.cshtml(编辑器模板)
@model Models.Junk
<div>
@Html.LabelFor(model => model.ID)
@Html.EditorFor(model => model.ID)
</div>
<div>
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
控制器:
// Called from jquery client
public string PerformMagic(string Id)
{
ViewModel model = dbContext.GetPersistedView();
model.Junk.Add(new junk);
// I need to update my view here but this doesn't update the view (model updates fine)....
// ...after a refresh on the browser of course the new junk in the model is displayed.
return RedirectToAction("Index", model);
}
【问题讨论】:
-
“这不起作用” 你能说什么不起作用吗?发生什么了?您是否通过
ajax请求到达此操作??? -
是的。我是对的...请参阅下面的答案。我的水晶球就像一个魅力!
标签: jquery ajax asp.net-mvc asp.net-mvc-3 redirect