5

I have a List of patients. One of the properties of the Patient class is PatientId which is an int. My list is initialized like:

List<Patient> = new List<Patient>() { new Patient { PatientId = 1, 
                                                    FirstName = "Jane", 
                                                    LastName = "Doe"}};

When I access PatientId in my view with something like:

@Html.DisplayForModel(modelItem => modelItem.PatientId) //Error

I get an error: Cannot convert lambda expression to type 'object' because it is not a delegate type

1
  • 5
    have you tried @Html.DisplayFor(modelItem => modelItem.PatientId)? Commented Apr 26, 2013 at 18:19

2 Answers 2

5

DisplayForModel actually uses Display Templates to display your model and is normally called without parameters. If this is what you intended then you will have to make a new DisplayTemplate. For anyone interested, here is a nice tutorial by Phil Haack on Display Templates and DisplayForModel: http://haacked.com/archive/2010/05/05/asp-net-mvc-tabular-display-template.aspx.

However, if you just wanted to have a label for that field, then you should just use DisplayFor

@Html.DisplayFor(modelItem => modelItem.PatientId)
Sign up to request clarification or add additional context in comments.

3 Comments

OK, When I auto-generated my view, it put everything in except PatientId, so it put in @Html.DisplayForModel(modelItem=>modelItem.FirstName). I am still confused on why it does not work for int's.
OK, my bad I am an idiot, I don't know why I was seeing DisplayForModel, it has DisplayFor on everything. Time for new glasses. How is the chess coming along?
@Xaisoft - Really threw me for a loop there, I could not find out why DisplayForModel was autogenerated for ints :P The chess is good.
3

You've confused between @Html.DisplayFor() and @Html.DisplayModelFor(). The former takes in a lambda. In your case, you could use @Html.DisplayFor(model => model.PatientId)

1 Comment

Thanks, I was seeing DisplayForModel, but it was actually DisplayFor.

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.