0

In my MVC application i created the view Index.cshtml and there i declared the C# variable..

Eg: string selectedvalue="";

now how to use it in the javascript function code which i wrote with in this Index.cshtml, so that i can append some text to this string in javascript function?

string selectedvalue=""; // Coding Part function onchangeFT(e) { '@selectedvalue'= e.value; //e.value come form combobox and it is should be assigned to "selectedvalue" alert('@selectedvalue'); }

It is giving me error...

any idea to solve this issue?

P_A_1
  • 93
  • 2
  • 8

1 Answers1

0

I think you are confused about what you are doing in this case.

You have declared the C# variable @selectedValue using MVC razor syntax. You can then use this variable to inject it's value into the content that is output to the client. However, you cannot assign back to this variable from client side javscript code as it does not exist. Your error will be in relation to the fact that you are actually attempting to assign an object value back to a literal string declaration which is invalid.

In your example

'@selectedValue'=e.value;

would translate to

'' = e.value;

This is because MVC razor will automatically translate the c# param into it's value and output it literally. In order to assign a variable to the e.value you should create a javascript value as part of your script block which your script will then identify as usable target variable.

For instance:

var selectedValue = '';
selectedValue = e.value;
Brian Scott
  • 9,221
  • 6
  • 47
  • 68