Question

I'd like to create like a fabric of ActionResult's based on the user choice.

My question is if it could be done in a more generic way or at least improvised somehow?

I just to make sure that it has done well.

Here is my code.

<p>A Partial Control that is initialized on Server-Side</p>
@{
    <h2>@ViewBag.InitializeUserControl</h2>
    Html.RenderAction("ShowGadget",new { actionName = (string)@ViewBag.InitializeUserControl } );      
}

 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            @ViewBag.InitializeUserControl = "InitializeUserControl2"; // IT GOES FROM A DATABASE
            return View(new HomeModel());
        }

        public ActionResult ShowGadget(string actionName)
        {
            var gadgetPresenter = new GadgetPresenter();
            return gadgetPresenter.GetActionResult(actionName);
        }
    }


    public class GadgetPresenter : Controller
    {
        public ActionResult GetActionResult(string actionName)
        {
            if (string.IsNullOrEmpty(actionName))
            {
                return DefaultUserControl();
            }
            else
            {
                if (actionName.Equals("InitializeUserControl"))
                {
                    return InitializeUserControl();
                }
                else if (actionName.Equals("InitializeUserControl2"))
                {
                    return InitializeUserControl2();
                }
                else
                    return DefaultUserControl();
            }
        }

        public ActionResult InitializeUserControl2()
        {
            ColorModel colorModel = new ColorModel
            {
                Width = 100,
                Height = 100,
                RGBColor = "#FF0000"
            };

            return PartialView("UserControls/ColorBlockUserControl2", colorModel);
        }

        public ActionResult InitializeUserControl()
        {
            ColorModel colorModel = new ColorModel
            {
                Width = 200,
                Height = 200,
                RGBColor = "#FF0000"
            };

            return PartialView("UserControls/ColorBlockUserControl", colorModel);
        }

        public ActionResult DefaultUserControl()
        {
            return PartialView("UserControls/DummyControl");
        }

    }
Was it helpful?

Solution

I think I understand where you're going with this, but I think you might be force feeding the wrong pattern. So, seeing as this will be a value from your database, you need to ensure a few things:

  1. That if a related item is there or not there, your app fails gracefully.
  2. That you're using convention over configuration to aid maintainability.

The pattern/technique I think you need is a mobile view engine. Take this example of Scott Hanselman's Mobile View Engine for MVC3 (source) as an example of this. Note how the engine looks for a .mobile.cshtml view and falls back to the plain .cshtml view if it doesn't exist. Obviously, this is now built into MVC4, but the technique is a viable one for a variety of purposes.

You could adapt this to your needs, looking to a session variable or similar (Singleton over Session would be my method) for your stored database value related to your customer to get the view prefix and provide a different view prefixed specifically for it.

The fallback is graceful and the pattern is all about convention and injection. It's certainly the approach I would take to solve your problem. I hope this has been of some help, whether you end up using the technique or not. Good luck and Godspeed!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top