0

i tring to do the function CityService.GetCityName , this function get CityID and give back the CityName. when i push on the function some number it is working but when i put var or somthing like this that put error and a red underline on the var. i tried to do console.log(cityID) and it was good id,and int.

var cityID = parseInt(<%=((User)Session["User"]).CityID%>);  
 $("#OrderLocation").text("<%=CityService.GetCityName(cityID)%> <%=((User)Session["User"]).UserLocation %>");

some help please ?

2
  • What does the error say (red underline) Commented Feb 28, 2018 at 1:25
  • @FurkanKambay prntscr.com/ikotqj Commented Feb 28, 2018 at 1:27

2 Answers 2

1

You've mixed and matched client-side and server-side variables here.

Whenever you're using the <% %> syntax around something, this indicates code and variables that are server-side. It is compiled and executed on the server first, and then returned to the client along with the rest of the content.

However, the "var cityID" is a client-side variable within the JavaScript. This variable doesn't exist within your .NET code -- it doesn't get evaluated until after the content has been processed and sent to the client's browser, when the JavaScript executes.

In this case, what you really need to do is change this around so that you are evaluating cityID as a .NET server-side variable instead of a JavaScript client-side variable. That way, the variable will be available to your server-side code, and your error should go away.

Sign up to request clarification or add additional context in comments.

Comments

0

In the first line you create a Javascript variable

var cityID = parseInt(<%=((User)Session["User"]).CityID%>);  

You can't use variables of Javascript in the Backend

Suggestion:

 $("#OrderLocation").text("<%=CityService.GetCityName(int.Parse((User)Session["User"]).CityID))%> <%=((User)Session["User"]).UserLocation %>");

Comments

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.