locked
How can I just loop through my linq result without involving/creating a model? RRS feed

  • Question

  • User1901201124 posted

    Hi there, 

    Basically in my controller I have this method.

            public ActionResult SomeMethod()
            {
                // Binds the Client drop down list for the search 
                ViewBag.Clients = new SelectList(db.Clients, "ID", "Name");
    
                var LQ =  (
                   // My linq code here.
                ).ToList();
    
                return View(LQ);
            }

    Then all I want to do in my .cshtml page is be able to pass that LQ through with the option selected in the drop down list and loop through the result of LQ.
    But it requires me to define a model? Something like the below?

    @model IEnumerable<something.something>

    I just want to be able to loop through the LQ without involving a model since there is no model for it. I am doing some counts in LQ where its not model specific.

    Thanks, any help would be appreciated! 

    Thursday, July 4, 2019 1:52 PM

All replies

  • User475983607 posted

    I just want to be able to loop through the LQ without involving a model since there is no model for it. I am doing some counts in LQ where its not model specific.

    Pass the type using the ViewBag if you do not want to use a Model.

    Thursday, July 4, 2019 2:22 PM
  • User1901201124 posted

    Thanks, I started doing that and it seems OK. But because my linq query has:

    ...
    Select new
    {
         ClientCount = //my query
         ContractCount = //my query
    }
    ...

    I am wondering how I would be able to refer to those variables? The below caused an error and says 'object' does not contain a definition for 'ClientCount'

        @if (ViewBag.Results != null)
        {
            foreach (var item in ViewBag.Results)
            {
               <li>@item.ClientCount</li>
            }
        }



    Thursday, July 4, 2019 2:29 PM
  • User753101303 posted

    Hi,

    C# is strongly typed and see that your object item doesn't have this property. Instead try :

    foreach (dynamic item in ViewBag.Results)

    It make me think you could also use @model IEnumerable<dynamic>and if you @model was introduced before ViewBag, you would likekly not need ViewBag at all...

    Thursday, July 4, 2019 3:04 PM
  • User1901201124 posted

    Thanks for the reply!  I think I got it to work. Well for the basics of what I want anyways! Thanks everyone for the help.

    I created a public class "ResultSet".

    public class ResultSet
    {
       public string FiscalYear { get; set; }
    }
            public ActionResult SomeMethod()
            {
                var statsModel =  (
                    ...
                    select new ResultSet
                    {
                        FiscalYear = g.Key.FYear,
                    ....
                    }
                ).ToList();
    viewBag.MyResults = statsModel; return View(); }

    In my .cshtml file I need to figure out how to set it up so i can loop through items.

    @{
        var infos = (List<ResultSet>)ViewBag.MyResults;
    }





    Thursday, July 4, 2019 4:05 PM
  • User1520731567 posted

    Hi MVCNewbi3v,

    Yes.

    You could also use model bind,like

    public class ResultSet
    {
       public string FiscalYear { get; set; }
    }
    public ActionResult SomeMethod()
            {
                var statsModel =  (
                    ...
                    select new ResultSet
                    {
                        FiscalYear = g.Key.FYear,
                    ....
                    }
                ).ToList();
                viewBag.MyResults = statsModel;
                return View(statsModel); //pass data which type is ResultSet model
            }

    and then,add strong type model in view:

    @model IEnumerable<XXX.XXX.ResultSet>

    ....html...

    In addition,I suggest you could add a scaffolded item to research its template.

    You could follow the steps below:

    Right click on Controllers -> Choose Add -> Choose 'MVC5 Controller with views, using Entity Framework', then Add-> Choose the Model (ResultSet Model) you want to add CRUD to in 'Model class', then Add -> The controller with CRUD function is created.

    Then the  Index,Details,Edit ..pages will be automatically generated.

    Best Regards.

    Yuki Tao

    Friday, July 5, 2019 6:26 AM