0

I have several Razor functions at the beginning of my _SiteLayout.cshtml file that I thought would be accessible by pages using it for their layout. However it doesn't seem to work that way... Is there any way for other pages to utilize them?

Edit: Here's an example of one of the functions. If I define the function like you mentioned will I be able to call it by using it's name? Ex. SaveQuestion(3, someVariableHere)

 public void SaveQuestion(int QID, string A)
    {
        if (A != "")
        {

            var db = Database.Open("DB2");

                var save = "INSERT INTO QuestionnaireData (QuestionID, ID, Answer) VALUES (@0, @1, @2)";
                db.Execute(save, QID, WebSecurity.CurrentUserId, A);

            }
        }

Something like:

    public static class GlobalFunctions
{
    public void SaveQuestion(int QID, string A)
        {
            if (A != "")
            {

                var db = Database.Open("DB2");

                    var save = "INSERT INTO QuestionnaireData (QuestionID, ID, Answer) VALUES (@0, @1, @2)";
                    db.Execute(save, QID, WebSecurity.CurrentUserId, A);

                }
            }
}

And then something like GlobalFunctions.SaveQuestion(3, someVariableHere) to call it?

Blake
  • 756
  • 3
  • 16
  • 34
  • 1
    Can they be implemented as extension methods to the HtmlHelper class? This would let you access them by using the Html object that is accessible to all Razor files. – Katie Kilian Oct 28 '14 at 21:28
  • @CharlieKilian Possibly, I don't really understand a lot of the guts behind ASP.NET and I feel like I could mess something up very easily. Is there no way to create some sort of file that has global access by the rest of the site? – Blake Oct 28 '14 at 21:32
  • If they don't involve HTML (or even if they do), you can create a static helper class in your project and type `@using ` in your pages and use the functions. Although this would work for HTML too, I dislike seeing HTML tags in strings because the lack of organization, but it would work too. – Danicco Oct 28 '14 at 21:34
  • The HtmlHelper extension methods seem like the most standard way of doing this. For me, it would depend on how many functions we're talking, and how heavy they are. But if there are just a few of them, I'd stick with the HtmlHelper extensions. (And if there are more than a few, why is this being done in C# instead of on the client in CSS, anyway? But I may just misunderstand the nature of your functions.) – Katie Kilian Oct 28 '14 at 21:36
  • @CharlieKilian There aren't too many, 20 or less I'd say. They have to be run server side because some make calls to the database. – Blake Oct 28 '14 at 21:39
  • @Danicco That seems like it would make the most sense. I think I'll look into that more... – Blake Oct 28 '14 at 21:43

1 Answers1

0

Try putting them in their own static class, implemented as extension methods to the HtmlHelper class. Like this:

public static class LabelExtensions
{
    public static string Label(this HtmlHelper helper, string target, 
         string text)
     {
         return String.Format("<label for='{0}'>{1}</label>", target, text);
     }
}

Then in your Razor page, you use it like this:

@Html.Label("firstName", "First Name:")

That example is from this link at MSDN.

UPDATE

You shouldn't put a SaveQuestion() method in your Razor page. That functionality should go in the controller class. The Razor page is only producing the HTML that will be sent to the browser. Once the browser clicks a button, another request happens, and during that request, the controller should call the SaveQuestion() method as appropriate. At that point, you can put that code anywhere and include it with a using statement that references whatever namespace you put it in.

Katie Kilian
  • 6,815
  • 5
  • 41
  • 64
  • I'm a little confused. The functions I'm using aren't just HTML helper functions. They perform calculations based on information they are retrieving from a database. If I define them like you show above will I be able to call them just using their name on other pages? See edit to question... – Blake Oct 28 '14 at 21:58