gpt4 book ai didi

jquery - 如何调用 RedirectToAction 刷新 View ?

转载 作者:行者123 更新时间:2023-12-01 03:00:50 27 4
gpt4 key购买 nike

如何在调用重定向(或等效方法)的 Controller 方法中刷新 View ?

查看(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>

Controller :

// 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 请求。

您无法重定向 ajax 请求!你应该做的是返回模型的 Json 值并在客户端更新页面:

public string PerformMagic(string Id)
{
ViewModel model = dbContext.GetPersistedView();
model.Junk.Add(new junk);

return Json(model);
}

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);...
}
})
}​

关于jquery - 如何调用 RedirectToAction 刷新 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10115132/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com