在MVC5中有@Ajax.ActionLink
这对于仅更新部分视图而不是重新加载整个视图很有用。显然在 MVC6 中不再支持。
我尝试过使用@Html.ActionLink
如下所示,但它不会更新表单,它仅返回部分视图:
View:
@Html.ActionLink("Update", "GetEnvironment", "Environments", new { id = Model.Id }, new
{
data_ajax = "true",
data_ajax_method = "GET",
data_ajax_mode = "replace",
data_ajax_update = "environment-container",
@class = "btn btn-danger"
})
control:
public async Task<ActionResult> GetEnvironment(int? id)
{
var environments = await _context.Environments.SingleOrDefaultAsync(m => m.Id == id);
return PartialView("_Environment",environments);
}
部分视图:
@model PowerPhysics.Models.Environments
this is a partial view
然后我尝试使用 ViewComponents。当页面加载时,组件工作正常,但我不明白如何随后刷新组件(例如使用按钮):
View:
@Component.InvokeAsync("Environments", new { id = Model.Id }).Result
成分:
public class EnvironmentsViewComponent : ViewComponent
{
public EnvironmentsViewComponent(PowerPhysics_DataContext context)
{
_context = context;
}
public async Task<IViewComponentResult> InvokeAsync(int? id)
{
var environments = await _context.Environments.SingleOrDefaultAsync(m => m.Id == id);
return View(environments);
}
}
如何使用 MVC6 中的部分视图仅更新视图的一部分?