Actionlink to child action

拟墨画扇 提交于 2020-01-04 03:13:23

问题


I have called a view from another with Html.Action method. I want to call the same action with a parameter Inside the child view, when user click the action link.

When I write this code I get this error message:

Html.ActionLink("link", "Configure", new { id = 2 })

The action 'Configure' is accessible only by a child request.

How can i handle this issue?

Edit: I'll try to reexplain the issue:

my parent view is ConfigureMethod.cshtml. I call child child view like that:

@Html.Action("Configure", "Payment");

It goes to this controller and returns actionresult(not partialview) inside ConfigureMethod view:

[ChildActionOnly]
public ActionResult Configure()
{

}

inside configure view i make an action link like that:

Html.ActionLink("link", "Configure", new { id = x.Id })

It should goes to this controller:

[ChildActionOnly]
public ActionResult Configure(int Id)
{

}

However when childonly attribute is written it gives error. When I remove this attribute it works but results comes directly, not inside ConfigureMethod view.

thanks.


回答1:


There are two things I would like to point out,

  1. Child actions are only meant to be rendered as PartialView. So decorating with [ChildActionOnly] attribute means, this is the action must return PartialViewResult.

  2. When we are calling ActionLink(), it will generate a link to a View not Partial View. Even if you will not decorate with [ChildActionOnly], then a link to a partial view makes no sense.

So, first decide if you want a View or Partial View and then design accordingly.



来源:https://stackoverflow.com/questions/8866920/actionlink-to-child-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!