html button link in ASP.NET MVC

I have a asp.net mvc application using C# and razor syntax.

So on one of my views I have the following razor link. If I click on this link I'm taken to the Index.cshtml view which is in the Home folder inside the Views folder. So that link works correctly.

@********* link display text - View (name of cshtml file) - name of folder inside the Views folder *********@
@Html.ActionLink("GoToIndexPage", "Index", "Home")

Is it possible to instead of having a razor link on this page, to instead have a regular html button like  this.
When I click on this button link I want it to go to the same Index.cshtml  page as the razor link above.

I tried something like this but doesn't work.
Is this possible? if so anyone know the correct syntax?

<input id="BtnTest1" type="button" value="Back" class="ButtonStyle" onclick="window.location.href = 'index.cshtml'" />
LVL 1
maqskywalkerAsked:
Who is Participating?
 
Lokesh B RDeveloperCommented:
Hi,

You can use Url.Action for doing this on html button

Home - Controller Name
Index - Action Name

<input id="BtnTest1" type="button" value="Back" class="ButtonStyle" 
onclick="window.location.href='<%= Url.Action("Index", "Home") %>';" />

Open in new window

0
 
SriVaddadiCommented:
In asp.net mvc, you cannot access the view page directly. There is a web.config file in the views folder which has settings restricting direct access to the view.

You either do this

<input id="BtnTest1" type="button" value="Back" class="ButtonStyle" onclick="window.location.href = '/Home/Index'" />

correct approach

or

You should remove the web.config which is a security issue as any one could access your view directly. Not a good idea.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.