Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Adriano Fabri 469 posts 1633 karma points
    Feb 15, 2017 @ 11:20
    Adriano Fabri
    0

    How to share functions between partial views

    Hi to all, I need to create a partial view (macro?) that must have only functions to use in other partial views. For Example:

    @functions
    {
        public string test1(string textString)
        {
            string x = "Speak: " + textString;
            return x;
        }
    
         public string test1(string textString)
        {
            string x = "Write: " + textString;
            return x;
        }
    }
    

    I tried with:

    @{ Html.RenderPartial("~/Views/MacroPartials/CustomFunctions.cshtml"); }
    

    But when I try to use a function, I receive this error:

    error CS0103: The name 'test1' does not exist in the current context
    

    I need to use this method because I haven't access to the App_Code folder. Can anyone help me?

  • sahadev 18 posts 145 karma points
    Feb 15, 2017 @ 12:03
    sahadev
    0

    hello, as per my knowledge you cab not able to access one view function into another ,so solution is you need to find reason why you are not able to access "App_code folder "

    can you please tell me why you are not able to access "App_code" folders classes code?

    so i will help you solve problem

  • Adriano Fabri 469 posts 1633 karma points
    Feb 15, 2017 @ 13:23
    Adriano Fabri
    0

    I haven't free access to website folder. Everytime I must change something on FS, I need to ask for access. It is a very long and exhausting procedure.

    So...the problem is that one of the shared methods must be changed frequently and this method is used by various partials. I would find a way to simplify the future modification of this method.

    I hope I was clear Adriano

  • sahadev 18 posts 145 karma points
    Feb 15, 2017 @ 13:39
    sahadev
    0

    can you please tell me what kind of method you want to use in different different partial views so can you help you in that ?

  • Adriano Fabri 469 posts 1633 karma points
    Feb 15, 2017 @ 14:07
    Adriano Fabri
    0

    This is a very symple one. This function associate a color to a specific css class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Newtonsoft.Json.Linq;
    using Umbraco.Web;
    using Umbraco.Core.Models;
    using USN.UrlPicker.Umbraco.Models;
    
    namespace CustomLogic
    {
        public static class CustomHelper
        {
    
        public static string NormalizeColorName(string color)
        {
            switch (color.ToUpper())
            {
                case "000000":
                    color = "cssClassBlack";
                    break;
                case "EE395C":
                    color = "cssClassRed";
                    break;
                case "728EA4":
                    color = "cssClassGrey";
                    break;
                case "94BCE2":
                    color = "cssClassCyan";
                    break;
                case "4AC3D9":
                    color = "cssClassAcqua";
                    break;
                case "FAAC04":
                    color = "cssClassOrange";
                    break;
                case "9FD74E":
                    color = "cssClassGreen";
                    break;
                case "177CCA":
                    color = "cssClassBlu";
                    break;
                default:
                    color = "default";
                    break;
            }
    
            return color;
        }
      }
    }
    

    This is a very simple method but there are more complex methods I need to share.

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Feb 19, 2017 @ 23:09
    Alex Skrypnyk
    0

    Hi Adriano

    I know that this is little bit weird suggestion but what if you will upload changes to web-site via Umbraco packages?

    Just create a new package, and needed dll, publish at Umbraco Package Repository and use it in your site via Umbraco admin part.

    Is it a solution of your problem?

    Thanks,

    Alex

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Feb 28, 2017 @ 13:43
    Alex Skrypnyk
    0

    Hi Adriano

    Did you find a way to do it?

    Was it solved ? Can you share please?

    Thanks,

    Alex

  • Stefano 61 posts 313 karma points c-trib
    Feb 28, 2017 @ 14:56
    Stefano
    0

    I think I'd solve it with helper methods, but if you are saying you don't have free access to the website/can't add helper methods I'm not sure you can share those :-/

    Maybe you can look into aggregating at least some of the partials? Making it more generic so you merge two or more partials together?

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Feb 28, 2017 @ 16:53
    Alex Skrypnyk
    0

    Hi Stefano

    Can you show how to aggregate some of the partials?

  • Stefano 61 posts 313 karma points c-trib
    Feb 28, 2017 @ 18:15
    Stefano
    0

    That'll depends by what exactly the page does, I'm just saying there could be the case when 2 templates or partials are very similar and they can be made into one!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies