如何从局部视图调用控制器动作方法 [英] how to call controller action method from partial view
问题描述
我关注了_PartilView.cshtml
I have following _PartilView.cshtml
@model= Project.ViewModels.UserViewModel
@foreach (var item in Model.Users)
{
<li data-thumb="@Url.Action("Sample", "Image", new { imageid = item.ImageValue})"></li>
}
但这不是在Image
控制器中调用Sample
方法的方法,因为我不能在PartialView中使用JavaScript,哪种方法可以更好地解决此问题?
but this is not call to the Sample
method in Image
controller, since I cant use JavaScript in PartialView, what the approach would be better to solve this ?
推荐答案
使用@ Html.Action而不是@ Url.Action
Use @Html.Action not @Url.Action
@model= Project.ViewModels.UserViewModel
@foreach (var item in Model.Users)
{
<li data-thumb="@Html.Action("Sample", "Image", new { imageid = item.ImageValue})"></li>
}
@ Url.Action()
@Url.Action()
它根据提供的操作名称,控制器名称,路由对象生成URL. 它包含8个重载方法.
It generates a URL as per provided action name, controllername, route object. It contains 8 overload methods.
@ Html.Action()
@Html.Action()
它在控制器中调用子操作,并返回Html字符串作为结果. 它包含6个重载方法.
It calls child action in a controller and return Html string as result. It contains 6 overload methods.
这篇关于如何从局部视图调用控制器动作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!