简体   繁体   中英

Acessing specific variable in ASP.NET Razor

Hey I hope this question isn't too stupid.

How do you acess only one Value of a page. For example I have 3 variables in my cshtml file.

@{var number1 = a1;}
@{var number2 = b2;}
@{var number3 = c3;}

but if I use GET in my application downloads all of the information and I can't seperate it anymore. With this url

"http://www.myurl.com/numbers.cshtml"

The outcome is "a1b2c3".

Is it possible to acess only the value of eg number2? So that the outcome would be "b2"? I searched for hours and I think it has to do with the url so that I type in my application

"http://www.myurl.com/numbers.cshtml?number2"

Obviously this doesnt work. I hope you understand what I mean. I would really appreciate some usefull answers :)

Greetings

Alex

//EDIT

Hey thanks for the answers. I'm sorry if I wasn't clear enough. Basicly I want to GET data from my serverside files via Unity3D (Game Engine). I want that as soon as you log in the game downloads all the information about your profile (name, level, experience etc). The problem is, that Unity downloads the content of a whole URL. I can't acess any values or variables. It's output is pure text. Now of course I could write for each text field its own script but I prefer to have certain groups in one file. Basicly I download the values from my DB and then to Unity3D. As it downloads everything I need to download only the ones I need right now. For example I want to download only the players name but as it's in the same file I need to tell unity somehow that I only want a specific variable. I thought this would be possible by using a modified URL. If I'm wrong and this is total nonsense I'm very sorry.

//EDIT2:

The Code inside the application:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public string url = "http://localhost:19206/ListProducts.cshtml";

 IEnumerator Start()
    {
        WWW www = new WWW(url);
        yield return www;
        guiText.text = www.text;
    }

Unfortunatly something like www.number1.text didn't work. Thank you so much again!

//EDIT3

and finaly my simple and basic code of my asp.net razor file. There is no DB implementation yet but I guess that has nothing to do with it.

@{ var experience = 3532; }
@{ var playerName = "Player1"; }

Your current experience is: @experience
Your Playername is: @playerName

Consider simply returning JsonResult from the action instead of coming up with custom view.

 return JsonResult({number1=42, text1="bla", number2=7});

And on JavaScript side you'll get {number1:42, text1:"bla", number2:7} which is very JavaScript friendly.

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