gen*_*eek 2 ajax asp.net-mvc jquery redirect asp.net-mvc-3
如何在调用重定向(或等效)的控制器方法中刷新视图?
查看(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() },
})
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
控制器:
// 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);
}
Run Code Online (Sandbox Code Playgroud)
看起来你正在提出ajax请求.
您无法重定向ajax请求!你应该做的是返回模型的Json值,并在客户端更新页面:
public string PerformMagic(string Id)
{
ViewModel model = dbContext.GetPersistedView();
model.Junk.Add(new junk);
return Json(model);
}
Run Code Online (Sandbox Code Playgroud)
jQuery的:
onComplete: function(event, queueID, fileObj, response, data) {
$.ajax({
url: '@Url.Action("PerformMagic","Home")',
data: {
first: $("#fname").val()
},
success: function(response) {
$('#First').val(response.First);
$('#Last').val(response.Last);
$('#Junk').val(response.Junk);...
}
})
}?
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2498 次 |
| 最近记录: |