Skip to content Skip to sidebar Skip to footer

Place Javascript Variable Within Razor Code

I have one JavaScript function which adds a row into the table with the selected value in a text box. But how will i place the selected value inside the razor code ? var val = docu

Solution 1:

No it is not possible, because .NET MVC is server side code and is evaluated before being sent to the client, and javascript is Client side code which runs only once it is ON the client.

Actually you can do one thing you can get the element by using Id.

@Html.TextBoxFor(x => x.extraDoc.dr_name, new {Value="Place Val Here", @class = "input1", @readonly = true })

Razor will generate id field for the TextBoxFor probably id will be something like this: extraDoc_dr_name (or) you can find it in browser developer tool.

then you can set it using javascript:

document.getElementById("extraDoc_dr_name").value = document.getElementById("tags").value;

Solution 2:

Try this:

var val = document.getElementById("tags").value;
var strHtml2 = '@Model.extraDoc.dr_name'  ;

Update:

Now i understand what you are trying to archive.

No, you cannot mix js variable inside razor code. You can assing a razor value to a js variable, but not a js variable to a razor variable(afaik)

Post a Comment for "Place Javascript Variable Within Razor Code"