0

How to generate javascript function with the name taken from ViewBag? I'm trying the below

@{ (ViewBag.ModuleId + "DatePickerStartDateChange = function() {console.log('asdf');};");}

and hoping that it will emit javascript in the html output as

xxxxxxDatePickerStartDateChange = function() {alert("asdf");};
1
  • 2
    You really shouldn't couple server & client code that way. Commented Mar 24, 2013 at 13:38

1 Answer 1

2

Absolutely no idea why on earth you would need to do that, but here's the correct syntax:

<script type="text/javascript">
    @(ViewBag.ModuleId)DatePickerStartDateChange = function() {
        console.log('asdf');
    };
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

I'm create a generic PartialView which will be used more than once in my View hence, in order for the javascript to work, i need to create saperate function name for each PartialView. Does that make sense? or there are better way of doing it?
No, this doesn't make any sense.

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.