dotnetnukedotnetnuke-module

DNN MVC module - How do I access settings?


I'm using Chris Hammond DNN module template to learn about DNN MVC module. I added another property in Settings class called Display and it is saved as "MessageList_Display" in the database and I get see the Display's value in Module's settings as well but How do I access this settings value?

I have a Model class called "MessagesViewModel.cs" and I have property called Message.

public string Message
{
    get
    {
        var settings = new Settings();
        if (settings.Display != null)
        {
            return settings.Display;
        }
        return string.Empty;
    }
}

The Settings's Display is null while Setting1 and Setting2 both have value. How do you access settings in DNN MVC module?


Solution

  • I have a working sample project for a DNN8 MVC module which includes a working module settings here: https://github.com/DotNetNuclear/DnnRestaurantMenu/releases

    Download the RestaurantMenuMVC_01.00.00_Source.zip package.

    I built this from the Chris Hammond template as well. I assume you have no issues setting and getting the module settings from the Settings Controller and view. If you are asking how to access the setting in your other views, you can access the value of your setting from your controller like this:

    public ActionResult Index()
    {
        string display;
        var model = new MessagesViewModel();
        // populate your model
    
        if (ModuleContext.Settings.ContainsKey("MessageList_Display"))
        {
            display = ModuleContext.Settings["MessageList_Display"].ToString();
        }
        ViewBag.Display = display;
        // or model.Message = display;
    
        return View(model);
    }
    

    Here you can see I added the setting to the ViewBag which I can access in my razor view. You could also set the value on your MessagesViewModel too.