The name 'xyz' does not exist in the curre

2020-02-06 17:36发布

This is probably really basic in C#, but I looked around a lot to find a solution.

In my MVC controller's action method, I have an incoming routeid (programid) that I need to use to create another string variable (accounttype). I have an if/else to evaluate the value of accounttype. Later in the same action method's code, I have nother if/else that takes the variable accounttype and creates a JSON string to pass to a payment gateway. But I get an error "The name 'accounttype' does not exist in current context.' Do I need to declare it as public or something?

Here are the two if/else statements:

if (programid == 0)
{
    string accounttype = "Membership";
}
else
{
    string accounttype = "Program";
}

Later on in same controller action, I need to use the accounttype variable to calculate another string variable (URL)

if (results.Count() > 0)
{
    string URL = accounttype + "some text"
}
else
{
    string URL = accounttype + "some other text"
}

7条回答
家丑人穷心不美
2楼-- · 2020-02-06 18:35

Your scoping is incorrect, try this:

string accounttype;
if (programid == 0)
{
    accounttype = "Membership";
}
else
{
    accounttype = "Program";
}
查看更多
登录 后发表回答