1

I'm trying to develop a simple partial view:

@model Nullable<DateTime>
@if (Model == null)
{
    <span>Active</span>
}
else
{
    <span>Resigned on @Model.GetValueOrDefault().ToShortDateString()</span>                                    
}

and use it from another view:

@Html.Partial("ViewTemplates/UserStatus",user.ResignDate)

The view works perfectly unless it is a Nullable<T>.

I receive the error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[Models.UserModel]', but this dictionary requires a model item of type 'System.Nullable1[System.DateTime]'.

Of course user.ResignDate is a nullable date time.

Any ideas?

Thanks

2 Answers 2

3

If model parameter passed to Partial method (user.ResignDate) is null, then asp.net-mvc passes caller view's model to Partial view, and you get error.

If you followed conventions ans placed UserStatus into DisplayTemplates folder, I would have reccommeded you to replace Partial call with DisplayFor method

@Html.DisplayFor(model => model.user.ResignDate, "UserStatus")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks archil but I don't have a model in the calling page (I am iterating on Model.Users and rendering user properties). Any ideas?
My code was just a guess :). You could do @Html.DisplayFor(model => user.ResignDate, "UserStatus"), but as I mentioned, only if UserStatus view is placed in DisplayTemplates folder
0

Please check data type of user.ResignDate it should be DateTime not nullable DateTime

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.