MVC 4 Razor - Naming javascript functions dynamically

call javascript function from mvc razor view
use @ javascript variable in razor
how to use javascript in mvc view
razor variable in html
call javascript function from cshtml
how to write c# code in html page
declare global variable in cshtml
razor javascript

I am trying to create the name of a function dynamically:

I set the name in razor code:

 @ { name="bob"; }

Then I try to create the javascript function, but the syntax is wrong.

function @name@:_onActivate() {
   .....
}

How can I use @name in the name of the function?

I know im late to the party, but i had the same question and came up with this..

Use the @() Razor syntax:

function @(Model.Name)_Init() { .....some code... }

Works like a charm

(http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/)

ASP.NET Razor C# Syntax, Main Razor Syntax Rules for C#. Razor code blocks are enclosed in @{ } Inline expressions (variables and functions) start with @; Code statements end with� Dynamically generated Javascript and CSS in ASP.NET MVC using Razor Views I was always wondering why ASP.NET allows to generate dynamically HTML (including inline CSS and JS), but does not allow to do the same for standalone JS and CSS files.

This was found to work in writing Javascript function names dynamically

@{
    @:function  
        @name<text>_onActivate() {
           ....
        }
   </text>
}

ASP.NET Razor C# Variables, Variables are named entities used to store data. Variables. Variables are used to store data. The name of a variable must begin with an alphabetic character and� Sets the namespace of the class of the generated Razor page, MVC view, or Razor component. Sets the root derived namespaces of a pages, views, or components classes from the closest imports file in the directory tree, _ViewImports.cshtml (views or pages) or _Imports.razor (Razor components). @namespace Your.Namespace.Here

There is also another option:

@{
     var functionStart = "function " + name + "_onActivate()";
}

@functionStart
{
    ...
}

This method doesn't make so much mess in Visual Studio in some cases.

ViewBag in ASP.NET MVC, ViewBag is a dynamic type so you can give any property name and value. in the previous section that the model object is used to send data in a razor view. So for this reason, MVC framework includes ViewBag. Example: Set ViewBag in Action method Net, JavaScript, jQuery, AngularJS, Node.js to your inbox. In above code I have three Javascript functions, first one is “GetDynamicTextbox()” that is useful for creating the HTML of control, second function “AddTextBox()” for adding the control on button click and third function “RemoveTextBox()” for removing the control on button click . Step 4: Run Application.

If your JavaScript is in the view (rather than its own js file - though I'm not saying I think this is advisable), you can try wrapping your JS code in <text> tags, so that the parser switches back to HTML mode (vs C# mode).

Another alternative may be not naming the function dynamically but instead passing in the name as a parameter - but whether that would work may depend on your design/implementation.

ASP.NET MVC - Using JavaScript with Ajax and Razor , NET MVC - Using Ajax Helpers with Razor Partial Views. On the Customer/Edit page, JavaScript functions are used to populate the Note that the standard naming convention for the libraries creates the potential for� As noted above, the convention for JavaScript associated with Razor partial views is to include them in the parent view .cshtml. JavaScript functions can be bound to elements on the partial view; the partial view is rendered at the same time as the parent view.

This worked well for me:

@{
    var someFuncName          = "someFuncName";
    var someFuncName2         = "someFuncName" + Model.type;
    var someDynamicFunc       =  Model.funcName;
    var someFuncNameComplete  =  "functionHelper()";
}

<script type="text/javascript">
    function @someFuncName@{<text>()</text>}{
        ...
    }
    function @someFuncName2@{<text>(data)</text>}{
        ...
    }
    function @someDynamicFunc@{<text>()</text>}{
        ...
    }
    function @someFuncNameComplete{
        ...
    }
</script>

ASP.NET MVC - Using Ajax Helpers with Razor Partial , Support for Ajax in the widely-used jQuery JavaScript library has used identifies the partial view, controller name, and HTTP form method. This is a beginner article which will help you to use JavaScript and jQuery library in Asp.Net MVC project. Calling JavaScript Function from Razor View. Assume, you have a list of employee displayed in HTML table and you want to call a javascript function on click of the employee name column.

MVC6: Razor code in dynamic JS/CSS files – Matthias Jauernig IT., Sometimes it would be really useful to include Razor/C# code in JavaScript or Take configuration values from web.config or URLs to MVC controller actions as an a „global“ dynamically generated JavaScript configuration file, that serves for MapRoute( name: "javascript", template: "JavaScript/{action}.js", defaults: new� Tag: javascript,jquery,css,asp.net-mvc-4,razor. I am trying to arrange news items in mvc4 razor with repetitive listing which wraps every two articles in a div class "news-group clearfix" and add a css element "last" to the second article class as shown below.

ASP.NET MVC, NET MVC – How to set HtmlAttributes programmatically in Razor As the name says, its members will be used to generate the HTML It's worth noting that this “ enhanced” capability for the two above methods has been added in ASP. Mistakes Developers make when Using Proxy API with JavaScript� The string stored in name is incorporated into a welcome message, which is passed to a JavaScript function, displayWelcome, which renders the welcome message into a heading tag. Call a void JavaScript function. JavaScript functions that return void(0)/void 0 or undefined are called with JSRuntimeExtensions.InvokeVoidAsync.

How to add control dynamically in Asp.net MVC using Javascript , How to add control dynamically in Asp.net MVC using Javascript. Oct 30, 2017 JavaScript, Add Control, Step3: Add view for action in controller & design. Right Click on Action Method > Add View > Enter View Name (Here I add Index) > Select View Engine (Razor) > Add. Now write the following code and Javascript I hope you are new to MVC. If you have a view model in the cshtml file, just use the view model and loop the same way to generate the controls dynamically. Ex: If your view model is Demo.ViewModels.EmployeeData foreach (Demo.ViewModels.EmployeeData emp in Model.EmployeeViewList) { < div > < @Html.TextBoxFor(model = > emp.EmpName) < @Html

Comments
  • This may help you stackoverflow.com/questions/14178430/…
  • what about to use a html helper that output the javascript function as a mvsString based on your model, something like @Html.DynamicJavascript(Model)