简体   繁体   中英

Javascript var inside a c# function inside Javascript

I am trying to write a javascript function that calls an Dictionary value and than tries to split it on a ´_'. The problem is that the Key to get the value from Dictionary is in an Javascript var that is extracted from a dropdownlist. Is it possible to use an Javascript var inside an c# function inside Javascript?

the Code as it is now:

var Category = document.getElementById('Properties').value;
var Name = document.getElementById(Category).value;
Category = '@Model.Dictionary[Name].Split('_')[0]';

EDIT:

People are misinterpreting my problem, the .Split function does work but the Dictionary[Name] is giving me problems. if i enter

var Category = document.getElementById('Properties').value;
var Name = document.getElementById(Category).value;
Category = '@Model.Dictionary["Bicycle"].Split('_')[0]';

into the code it works perfectly, but I want to get the "Bicycle" part dynamically out of an DropDownList.

EDIT 2:

Okay I have decided that what I am over thinking the situation and that what I am trying to do needs alot more work than is needed, found a beter solutions, thanks for everybody that tried to help me.

You're a bit mixed up between client and server side. To interact with server side code like this, you'll need to either post to the server or write an AJAX callback to call a server method.

http://mikehadlow.blogspot.com/2008/10/mvc-framework-and-jquery-ajax-heaven.html

No, that is not possible. All the server code runs before the page is sent to the browser, and all the client code runs after.

What you would have to do to use the dictionary in the client code is to create Javascript code from the server side, that recreates the dictionary on the client side.

If that's not possible, you would have to make an AJAX call to the server.

我已经通过使DropDownList在Value和Text中具有不同的值来完全避免了这个问题,这样做是可以从DropDownList的值中获取以前在Dictionary中所需的输入,而无需更改Text。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM