MVC: model of type Nullable<T>
I have a partial view that inherits from ViewUserControl<Guid?>
- i.e. it's model is of type Nullable<Guid>
. Very simple view, nothing special, but that's not the point.
Somewhere else, I do Html.RenderPartial( "MyView", someGuid )
, where someGuid
is of type Nullable<Guid>
. Everything's perfectly legal, should work OK, right?
B开发者_Go百科ut here's the gotcha: the second argument of Html.RenderPartial
is of type object
, and therefore, Nullable<Guid>
being a value type, it must be boxed. But nullable types are somehow special in the CLR, so that when you box one of those, you actually get either a boxed value of type T
(Nullable's argument), or a null if the nullable didn't have a value to begin with. And that last case is actually interesting.
Turns out, sometimes, I do have a situation when someGuid.HasValue == false
. And in those cases, I effectively get a call Html.RenderPartial( "MyView", null )
. And what does the HtmlHelper do when the model is null? Believe it or not, it just goes ahead and takes the parent view's model. Regardless of it's type.
So, naturally, in those cases, I get an exception saying: "The model item passed into the dictionary is of type 'Parent.View.Model.Type', but this dictionary requires a model item of type 'System.Guid?'"
So the question is: how do I make MVC correctly pass new Nullable<Guid> { HasValue = false }
instead of trying to grab the parent's model?
Note: I did consider wrapping my Guid?
in an object of another type, specifically created for this occasion, but this seems completely ridiculous. Don't want to do that as long as there's another way.
Note 2: now that I've wrote all this, I've realized that the question may be reduced to how to pass a null for model without ending up with parent's model?
<% Html.RenderPartial("MyView", someGuid ?? new Guid()); %>
UPDATE:
Using editor and/or display templates in ASP.NET MVC 2.0 you can achieve the desired result. Place a Guid.ascx
file in the Shared/EditorTemplates
folder and include it like this:
<%= Html.EditorFor(x => someGuid) %>
or if the guid is a property of the main model:
<%= Html.EditorFor(x => x.SomeGuid) %>
Now if you put a <%= Model.HasValue %>
inside the partial you can get false
but not with RenderPartial
.
精彩评论