c# - Accessing Controller variable in View MVC -
i have started use mvc , have set simple project has controller returns value entity framework. have controller index page visual studio setup default in template.
i hoping value returned index page of website.
controller code
misentities db = new misentities(); public actionresult index() { viewbag.message = "real time production"; var scrap = r in db.tbl_dppit select r.castgood; return view(scrap); } how access var scrap using razor? have looked @ viewbag method , other razor syntax cant seem find how access item.
just because controller variable declared using var keyword doesn't mean doesn't have type. rather, tells c# type should inferred context (in case, ienumerable<t> of whatever type tbl_dppit.castgood is).
so, other 3 answers partly correct, need define type of model being passed view via razor keyword @model noted other answers:
// in view razor: @model ienumerable</* type of castgood */> there alternative 3 answers specified, , sticking scrap variable viewbag in controller:
viewbag.scrap = scrap; when access expando property scrap viewbag in view, dynamic object, won't aid of intellisense. but, way, know possibilities.
update:
based on comments below, looks if castgood database column allowed null , type int, you'd want:
@model ienumerable<int?> hth.
Comments
Post a Comment