redirect from view to epi controller with parameter


Hi, i try to do somthing that is very simple in mvc, but now, with episerver i have some problems:

in the view, while clicking, i want to redirect to epicontroller and pass parameter.

in mvc i do something like 

@foreach (var element in Model)
<li> @Html.ActionLink(element.categoryName,"MyActionResult", "MyController")</li>


in mvc+epiServer, i already created the relevant epi page, how can i redirect to its  epi controller with parameter?

thank you

Oct 21, 2019 20:21

In order to post back to Episerver page Mvc controller - you have to generate link to page's url. Keep in mind that it's not controller route in classical Mvc world. You can name page whatever you want and in order to hit that controller - you need to post to that url.

One of the easiest way to achieve this is to use Html helpers - like `@Html.ContentLink(page)`.

You will need to get access to current page (one that was serving this request). It's usually available in view model (if you are using AlloyTech sample site approach).

Oct 21, 2019 22:15

Hi validis, 

if i use @Html.contentLink(startPage)

will the start page display or i get to the startpage controller?

the problem is i have  some logic to do in the controller, so i want to get to the startpage controller with parameter i send to the controller from the view.

is it possible?

Oct 21, 2019 22:56

Most of the time controller is required to display the view - so short answer - yes, controller should be hit prior displaying view

Oct 21, 2019 23:11

You could do:

<a href="@Url.ContentUrl(ContentReference.StartPage).AppendUrlParameter("foo", "bar")">Lorem Ipsum</a>

Where AppendUrlParameter is a new extension method:

public static string AppendUrlParameter(this string url, string key, string value)
    return url + (url.Contains("?") ? "&" : "?") + $"{key}={value.UrlEncode()}";

And get the markup:

<a href="/no/id4/?foo=bar">Lorem Ipsum</a>
Edited, Oct 22, 2019 7:04

Tomas, has great example. If you want to go further and have nice fluent API, you can install Geta's Episerver extension package. There is UrlBuilder class that gives nice approach to build urls (among other great helpers and extensions)..

Oct 22, 2019 8:14
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.