0

so I'm trying to understand how this works a bit better, but when I use

html.dropdownlistfor(model => item.Location ....

this is considered ok, but if I try to do

html.dropdownlistfor( () => item.Location ....

The function doesn't take it. Is there a particular reason this doesn't work?

James Joshua Street
  • 3,259
  • 10
  • 42
  • 80
  • In the first one, model is your screen model, so you are assigning values to model's property using item and lambda. The second one, () is an anonymous lambda, and it never assigns value to any of your model items. – SelvaS Jul 13 '18 at 15:05
  • so model is an actual named variable referring to the screen model? I don't think I've read this anywhere. Was it in the docs? So if I use some other named word, will it fail too? I thought that the lambda was passing in model but not using it since item.location is called instead. I thought that item was an enclosed variable from the closure as opposed to an actual input – James Joshua Street Jul 13 '18 at 15:16
  • Look at the Details.cshtml in this sample app : https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller It is declared `@model MvcMovie.Models.Movie` and used as `@Html.DisplayNameFor(model => model.Title)`. There is no explicit declaration for models. – SelvaS Jul 13 '18 at 15:25
  • I guess what I'm trying to understand is whether the "model" in the code you just wrote is a keyword or not. They seem to use ModelItem, model, etc interchangeably, so I'm guessing what you name the parameter is not important, it just gets passed the model based on the annotation at the top ("@model MvcMovie.Models.Movie"). However, I'm guessing that item is a keyword because a List is being passed to the view in the controller, correct? – James Joshua Street Jul 13 '18 at 16:05
  • Correct. Whatever you have declared with model during view initialization, you should use the same variable to bind the data not anonymous functions/lambda. – SelvaS Jul 13 '18 at 16:08
  • I guess what's confusing me is why in the documentation are there lines like this: @Html.DisplayFor(modelItem => item.Title) on the page you linked, I don't see anything defining a modelItem property, so I assumed that it didn't matter what I used for the name of the input property in the expression lambda – James Joshua Street Jul 13 '18 at 16:12
  • See this SO post and answers. https://stackoverflow.com/a/10098268/311255 – SelvaS Jul 13 '18 at 16:19

0 Answers0