FrankSharp
FrankSharp

Reputation: 2632

MVC Razor Call JS function in @Html.ActionLink

I'm trying to call the JS function like this: defaultDate = "test();" in my @Html.ActionLink. JS function never called...

@Html.ActionLink("Title", "Create", new { calendarId = Model.CalendarId, year = Model.Year , defaultDate = "test();" }, new { @class = "btn btn-primary btn-xs" })

What I'm doing wrong?

Upvotes: 1

Views: 177

Answers (1)

Andrew Shepherd
Andrew Shepherd

Reputation: 45232

When programming web code, you need to understand at which points various code blocks are run.

Think of it as two major steps:

Step 1: The server generates all of the html, css and javascript and send it to the client. In ASP.NET-MVC, you code this through razor files and controller methods.

Step 2: The browser (or client) renders the html and runs any javascript.

In your case, you are trying to generate a date and pass it as the defaultDate parameter to a server method. This is all part of step 1. Step 1 is performed independently of any browser. For this reason, you cannot run any javascript at this point.

Upvotes: 1

Related Questions