1

Problem resolved: I found a reusable solution. Thank you everyone for your help.

I'm frustrated and don't know what I'm doing. I'm attempting to build a webpage and one of the things I'm trying to do is cut down on code. In order to do so, I need to create a method, I know extensively about methods from Java, but not when trying to write C# in ASP.NET using razor syntax. The problem I'm facing is my @helper refuses to access the global hash table "dictionary". I've tried a lot of different things and have decided to turn to stackoverflow for help. Thanks in advance.

UPDATES:

Error Message is "CS0103: The name 'dictionary' does not exist in the current context"

I need a hashtable because I'm pulling from a database, checking to see if its null, if yes, replacing it with an empty string, then pushing it to a table. So if I could learn how to do it in this fashion, it would work best, I think?

<!doctype html>

@using System;
@using System.Collections.Generic;
@using System.Collections;
@using System.Linq;

@{
    var dictionary = new Dictionary<string, object>();

    dictionary.Add("fName", returnString100.FRSTNAME.Trim());

    @helper printOut(string toBePrinted) {
        object curValue;
        if(dictionary.TryGetValue(toBePrinted, out curValue)) { 
            return curValue; 
        }
    }
}

<table>
    <tr>
        <td>First Name:</td><td>@{ printOut("fName"); }</td>
    </tr>
</table>
4
  • Sounds like this logic doesn't belong in your view or in a helper anyway. Why don't you declare your dictionary inside the helper method? Commented Jul 21, 2014 at 14:28
  • 1
    This looks like too much code for a view. This should really belong in the model and the view would just reference a property of that model. Commented Jul 21, 2014 at 14:29
  • Please post the error that you get from this code. Commented Jul 21, 2014 at 14:29
  • Error message is "CS0103: The name 'dictionary' does not exist in the current context" Commented Jul 21, 2014 at 14:32

1 Answer 1

1

While philosophically I agree with the others that you might want to encapsulate this in a class, here's how you can do this in Razor with a view:

<!doctype html>

@using System;
@using System.Collections.Generic;
@using System.Collections;
@using System.Linq;

@{
    var dictionary = new Dictionary<string, object>();

    dictionary.Add("fName", returnString100.FRSTNAME.Trim());

    Func<string, object> PrintOut = delegate(string toBePrinted)
    {
        object curValue;
        if (dictionary.TryGetValue(toBePrinted, out curValue))
            return curValue;
        return "";   
    };

}

<table>
    <tr>
        <td>First Name:</td><td>@PrintOut("fName").ToString()</td>
    </tr>
</table>
Sign up to request clarification or add additional context in comments.

3 Comments

However I don't know where you're getting returnString100 from. I'll leave that up to you, as that wasn't part of the question :)
Would it be possible to skip all this and just pull straight from my hash table. Example dictionary.TryGetValue("fName").ToString() ?
Also I don't see how the answer allows me to reuse the code as there isn't a parameter involved. I'm pulling a single name, as it is a per user display, so will need to use this for multiple fields

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.