1

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?

2
  • 2
    onclick="test() in the html attributes - but its the 21st century, so use use Unobtrusive Javascript instead Commented Feb 22, 2016 at 21:51
  • @Html.ActionLink("Title", "Create", new { calendarId = Model.CalendarId, year = Model.Year, defaultDate = string.Format("{0:MM-dd-yyyy}", DateTime.Now) }, new { @class = "btn btn-primary btn-xs" }) Commented Feb 22, 2016 at 22:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.