I understand it is a bad practice to make use of database within the view. However, I am also passing the User object and that i question the way i makes it simple to use.

I really like the actual way it works in Ruby On Rails. You simply create an @instance_variable in before_filter and refer to it as in the remotes and in the sights.

You cannot do that in ASP.Internet MVC though. And So I produced a category with the data I have to pass towards the view (DataContext and User):

public class XData
{
    public DBDataContext DB { get; set; }
    public User User { get; set; }
}

In controller's Initialize method I recieve all of the data:

public XData X;

protected override void Initialize(RequestContext requestContext)
{
    base.Initialize(requestContext);

    X = new XData();

    X.DB = ...;
    X.User = ....;
}

Is effective: I'm able to obtain the database object in the view such as this:

<%= Model.X.DB.Users.First().Name %>

To be able to pass the information towards the view, I must perform the following:

    public ActionResult Foo()
    {
        return View(new FooModel
                        {
                            X = X,
                            HelloMessage = "Hello world!"
                        });
    }

The one thing I do not like here's which i also have to create the X = X factor. How do i initialize that instantly?

Thanks

You can place X in ViewData and write an HtmlHelper extension approach to access X or override the vista approach to the controller and give a little reflection logic that maps every instance property from the controller to qualities with matching names of the model (I suppose Automapper may help here...)

I have seen an identical problem where lots of remotes return an identical model with merely a couple of custom remaking. Within this situation, create an abstract base model that other models be a consequence of, along with a function that returns the specific model you'll need using the base X = X and so on already set.

For instance, think about this:

public abstract class MyBaseModel
{
    public User User { get; set; }
}

public class FooModel : MyBaseModel
{
    public string FooMessage { get; set; }
}

public class BarModel : MyBaseModel
{
    public string BarMessage { get; set; }
}

public class MyController : Controller
{
    public ActionResult Foo()
    {
        var model = this.GetModel<FooModel>();

        // Set the properties on FooModel.
        model.FooMessage = "Hello world!"

        return View(model);
    }

    public ActionResult Bar()
    {
        var model = this.GetModel<BarModel>();

        // Set the properties on BarModel.
        model.BarMessage = "Hello world 2!"

        return View(model);
    }

    protected T GetModel<T>() where T : MyBaseModel, new()
    {
        T model = new T();

        // Set the properties on MyBaseModel.
        model.User = ...;

        return model;
    }
}

If you wish to share MyBaseModel and GetModel among many remotes, extract it to a ModelProvider or something like that similar that's provided to every controller, ideally through dependency injection.

I think you will have to by causing your view strongly typed:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Namespace.XData>" %>

as well as in your controller:

 public ActionResult Foo()
    {
        var X = new XData();
        X.User = ....;
        X.SomeProperty = ...;
        X.Message = "Hello world!";
        return View(X);
    }

which enables you to definitely access the item inside your view like so:

<%: Model.User.UserName %>
<%: Model.Message %>