سؤال

I'm trying to create something like *.ascxs' factory.

Scenario: I would like to render controls which depends on model, which i've passed to partialView. I'd like to achieve something like this:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyAbstractModel>" %>

    <%= Model.Property1 %>
<!-- other more sophisticated displays on model -->

    <% Html.RenderAction("RenderControl", "Factory", new { model = Model});  %>

FactoryController:

public ActionResult RenderControl(object model) {
    if (model.GetType() == typeof(Model1) {
        return RenderPartial("Partial2", model);
    } else {
        return RenderPartial("Partial1", model);
    }
}

I'd like to know is there any better way to cope with such situation. I suppose It's not the most efficient method to build web page in ASP.MVC 2.

If this method is acceptable, how can i restrict access to such controller? I would like to use this class only on server side and only by ascxs' pages

هل كانت مفيدة؟

المحلول

Use the ChildActionOnly() attribute to restrict access to your actions.

What you are trying to do is already builtin to MVC: Html.DisplayFor()

See: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top