0

I'd like to keep in the database the list of the "Modules" which are in fact Partial Views of the ASP .NET MVC.

Is it possible to define that dynamically?

HTML

<p>A Partial Control</p>

@Html.Partial("UserControls/ColorBlockUserControl", new ColorModel())

<hr />

<p>A Partial Control that is initialized on Server-Side</p>
@{
    Html.RenderAction("InitializeUserControl");
}

C#

 public class HomeController : Controller
    {
        public ActionResult Index()
        { 
             return View(new HomeModel());
        }

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

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

I assume to use ViewBag to use like

 @ViewBag.InitializeUserControl = "InitializeUserControl"; // It goes from the database and can be ANY name


 Html.RenderAction(@ViewBag.InitializeUserControl);

But it is not working...

I hope u got the idea to define it

 Html.RenderAction("I need here the dynamic var");

Thanks!

P.S. To make it clear the final idea is provide to the user editable template (CKEditor) so user can add any ASP .NET MVC UserControl name i.e. "Gadget1" or "Gadget2" and we are able dynamically change WebPage and show all controls which have been added dynamically.

10
  • @Anon Yeah... There are some errors... CS1973: 'System.Web.Mvc.HtmlHelper<MVCColorUserControl.Models.HomeModel>' has no applicable method named 'RenderAction' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. Commented Aug 8, 2013 at 14:08
  • 1
    Any reason why it must be a partial view and not simply retrieved by Ajax? Commented Aug 8, 2013 at 14:09
  • Well, in your description you say they are partial views, but in the view your using: Html.RenderAction...Have you tried Html.Partial(@ViewBag.InitializeUserControl)? Commented Aug 8, 2013 at 14:10
  • @Anon Well. I did it. It doesnt work like u 've assumed. CS1973: 'System.Web.Mvc.HtmlHelper<MVCColorUserControl.Models.HomeModel>' has no applicable method named 'Partial' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. Commented Aug 8, 2013 at 14:14
  • Do you have any @using statements in your view? Commented Aug 8, 2013 at 14:15

1 Answer 1

1

Thanks to @satpal

He put his answer and after that deleted it. I don't know why. But he is right!

Html.RenderAction((string)@ViewBag.InitializeUserControl); 

This is working!

Sign up to request clarification or add additional context in comments.

Comments

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.