Reputation: 353
I am newbie in MVC.
In Razor I need to call a method inside a controller but its not working for me.
My Razor code is as below:
@helper SubmitCommnet()
{
@Html.Action("Post2", "PostShow", new { id = 1111 });
}
and my Controller is as follow:
public class PostShowController : SurfaceController
{
public ActionResult Index()
{
return null;
}
public ActionResult Post2(int id)
{
return null;
}
}
Any ideas why it not calling the Post2 method?
Upvotes: 0
Views: 1618
Reputation: 101604
You're using Html.ActionLink
like you would Url.Action
. Remember you need to specify the link text, too. Something tells me you want the following method:
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)
Then you can call it using:
@Html.ActionLink("Click Me", "Post2", "PostShow", new { id = 1111 }, null)
Unfortunately if you need to provide the action, controller & route arguments, you need the prototype with htmlAttributes
(however, you can simply pass null
).
Upvotes: 2
Reputation: 38598
With Razor
, you have to use a Html.ActionLink
to do this, which generates a hyperlink tag from html, or use the hyperlink tag directly, for sample:
@Html.ActionLink("Post", // <-- Text for UI
"Post2", // <-- ActionMethod
"PostShow", // <-- Controller Name.
new { id = 1111 }, // <-- Route arguments.
null // <-- htmlArguments .. which are none. You need this value
// otherwise you call the WRONG method ...
// (refer to comments, below).
)
Upvotes: 2