views:

414

answers:

1

Is there a way to use something like this: System.Web.Mvc.ViewUserControl<DateTime>? I get an exception that the type is a value type, not a reference type. What is the proper way to resolve this? Thanks.

Edit

What I am trying to accomplish is having a control that takes a DateTime to render a calendar. I want to pass in the DateTime from my ViewData using "dot notation" for MVC.

Edit 2

I heard/seen that some MvcContrib projects might have this capability, but I can't seem to find it again.

+1  A: 

There is no way to resolve this - only workarounds. You cannot include Value types as TModel in ViewUserControl as TModel has a constraint to be a reference type.

The easy workaround is to wrap your value type in a class as your model.

class MyModel {
  public DateTime? DateTime {get;set;}
}

By defining your own class like MyModel above, you can now pass a DateTime to your views, like so

ActionResult MyActionMethod() {
  var db = new MyDataContext();
  var dbThing = db.Things.Where(t=> t.DateTimeProperty>=DateTime.Now).First();
  return View("myView", new MyModel{DateTime = dbThing.DateTimeProperty});
}

Your view of course will need to define MyModel as it's model type, like so

public partial class MyView:ViewUserControl<MyModel> {
 //snip
}

And inside your View, simply refer to the DateTime property to access the DateTime.

<%=Model.DateTime%>
CVertex
This will not work for me.
Daniel A. White
It doesn't compile or you don't want to wrap in a class?
CVertex
I am using Linq to SQL
Daniel A. White
Oky..that's not what I asked. Hopefully my update answers your question
CVertex