c# - How do I use strongly typed models and @model? -
i'm learning asp.net mvc. have service folder have class reads data xml file. i've created controller think (?) should work, , i'm attempting create view reason can't intellisense autocomplete @model makes me think i've done wrong. additionally, when try access model properties view (eg model.description
- if syntax?) numerous missing { , } errors. doing wrong?
controller:
public actionresult index() { newsreader newsreader = new newsreader(); //read news file var newsitems = newsreader.getnewsitems(); return view(newsitems); }
and far have view:
@model ienumerable<testsite.services.news.newsitem> @{ layout = null; } <!doctype html> <html> <head> <meta name="viewport" content="width=device-width" /> <title></title> </head> <body> <div> <ul> @foreach(model.description) { } </ul> </div> </body> </html>
refer @erik funkenbusch explanation of mvc @model meaning
the @ sign directive tell razor engine follows code, , should compile rather write output.
so when type
@model blah compiled razor, , tells razor engine type of model 'blah', when use keyword model (note capital m , have use @ sign well) refer model have defined (in case blah).
therefore corrections should taken following:
//@model ienumerable<testsite.services.news.newsitem> @model ienumerable<testsite.services.news.newsitem> //@foreach(model.description) @foreach(var item in model.description) { }
i recommend read getting started asp.net mvc 5 learn basics asp.net mvc 5. razor engine used mvc 5 view styling.
Comments
Post a Comment