mvc4.0 html.actionlink comfired,asp.net mvc - Ajax.ActionLink alternative with mvc core - Stack Over...

本文探讨了在MVC6中使用PartialView进行局部视图更新的方法。文章首先介绍了MVC5中@Ajax.ActionLink的功能及其在MVC6中的缺失,并尝试通过@Html.ActionLink实现相似效果。此外,还讨论了使用ViewComponents进行局部刷新的可能性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In MVC5 there is @Ajax.ActionLink that is useful to update just a partial view instead of reloading the whole View. Apparently in MVC6 is not supported anymore.

I have tried using @Html.ActionLink like the following but it doesn't update the form, it return just the partial view:

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 GetEnvironment(int? id)

{

var environments = await _context.Environments.SingleOrDefaultAsync(m => m.Id == id);

return PartialView("_Environment",environments);

}

Partial view:

@model PowerPhysics.Models.Environments

this is a partial view

Then I tried using ViewComponents. When the page loads the component works correctly but I don't understand how to refresh just the component afterward (for example with a button):

View:

@Component.InvokeAsync("Environments", new { id = Model.Id }).Result

component:

public class EnvironmentsViewComponent : ViewComponent

{

public EnvironmentsViewComponent(PowerPhysics_DataContext context)

{

_context = context;

}

public async Task InvokeAsync(int? id)

{

var environments = await _context.Environments.SingleOrDefaultAsync(m => m.Id == id);

return View(environments);

}

}

How can I update just a part of a view by using PartialViews in MVC6?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值