0

I have started to use MVC and i have set up a simple project that has a controller than returns a value from entity FrameWork. I have a controller for the index page which visual studio setup as default in its template.

I am hoping that this value will be returned to the index page of the website.

Controller code

 MISEntities db = new MISEntities(); public ActionResult Index() { ViewBag.Message = "Real Time Production"; var scrap = from r in db.tbl_dppIT select r.CastGood; return View(scrap); } 

How do i access the var scrap using razor? I have looked at the viewbag method and other razor syntax but cant seem to find how i access this item.

    3 Answers 3

    5

    Just because the controller variable is declared using the var keyword doesn't mean it doesn't have a type. Rather, this tells C# that the type should be inferred by its context (in this case, an IEnumerable<T> of whatever type tbl_dppIT.CastGood is).

    So, the other three answers are partly correct, you need to define the type of model being passed into the View via the razor keyword @model as noted by the other answers:

    // In your view Razor: @model IEnumerable</* type of CastGood */> 

    There is an alternative to the three answers already specified, and that is sticking the scrap variable into the ViewBag in your controller:

    ViewBag.Scrap = scrap; 

    When you access the expando property Scrap from the ViewBag in the view, it will be a dynamic object, so you won't get the aid of IntelliSense. But, it is another way, just so that you know all the possibilities.

    UPDATE:

    Based on your comments below, it looks like if CastGood is a database column that is allowed to be null and whose type is int, then you'd want:

    @model IEnumerable<int?> 

    HTH.

    6
    • When i add the following "@model IEnumerable<MvcApplication1.Models.tbl_dppIT>" i get this error'The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery1[System.Nullable1[System.Int32]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApplication1.Models.tbl_dppIT]'.
      – Inkey
      CommentedAug 28, 2013 at 13:45
    • hmm, try @model MvcApplication1.Models.tbl_dppIT. That may already be an IEnumerable<T> from EF (or whatever ORM you're using).CommentedAug 28, 2013 at 14:08
    • still getting the error :/The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery1[System.Nullable1[System.Int32]]', but this dictionary requires a model item of type 'MvcApplication1.Models.tbl_dppIT'.
      – Inkey
      CommentedAug 28, 2013 at 14:13
    • OK, the simple answer is, you need to know the .Net type being returned from your query when you set the scrap variable in your controller. Whatever that type is, that's what type your @model needs to be. Either that, or use the ViewBag--but again, without IntelliSense support since it's a dynamic.CommentedAug 28, 2013 at 14:35
    • Looking at your code, I'd say your @model should be of type IEnumerable</* type of CastGood /*> since that's what's being returned by the LINQ query setting scrap in your controller. So, if CastGood is an int, then IEnumerable<int>. If CastGood is a concrete object, then IEnumerable<CastGood>, etc.CommentedAug 28, 2013 at 14:38
    1

    You need to declare the type of the variable scrap as the model inside the View using the @model keyword.

    // View.cshtml @model IEnumerable<Namespace.ScrapType> @foreach(var item in Model) { <p>@item.SomeProperty</p> } 

    See Part 3: Views and ViewModels of the MVC Music Store example.

      1

      You need to strongly type your view and it will available as the Model instance.

      @model IEumerable<MyType> @foreach(var item in Model) { @*do something with it *@ } 

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.