-1

I am working on a view which shows the table data in a table format in mvc. its shows an error that Model does not contain a definition for GetEnumerator.

MY VIEW

@model IEnumerable<TestComponents.Models.testorder>   
<!DOCTYPE html>
<html>
<head>

</head>
<body>   
    <table id="myTable">
        <tr class="header">
            <th style="width:60%;">TEST NAME</th>
            <th style="width:40%;">TEST ID</th>
        </tr>

@foreach (var items in Model)
{
        <tr> 
            <td>@items.searchtest</td>
            <td>@items.searchtestid</td>
        </tr>
}
    </table>

MY CONTROLLER

public ActionResult test()
{
    testorder to = new testorder();
    DbHandle dh = new DbHandle();
    to.searchtest = dh.searchtes('S', to);
    to.searchtestid = dh.searchtesid('S', to);
    return View("test", to);
}

MY DBHANDLE MODEL

public List<string> searchtes(char flag, testorder to)
{
    List<string> items = new List<string>();
    connection();
    SqlCommand cmd = new SqlCommand("SPNAME", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@PstrOperationFlag", flag);
    cmd.Parameters.AddWithValue("@Pstrtestname", 'w');
    con.Open();
    SqlDataAdapter sd = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    sd.Fill(dt);
    using (SqlDataReader sdr = cmd.ExecuteReader())
    {
        while (sdr.Read())
        {
            items.Add(sdr["testname"].ToString());

        }
    }
    con.Close();
    return items;
}

I don't know where I am getting wrong or why it doesn't getting retrieved from model to my view.

slavoo
  • 5,798
  • 64
  • 37
  • 39
Mark Antony
  • 39
  • 13
  • 2
    Your passing a single `testorder` to the view, not a collection of `testorder` so you would be getting a different exception - [this one](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) - so you have not even shown the correct code. And you would need to iterate through `Model.searchtest` which is a collection, not `Model` –  Jun 28 '17 at 05:17
  • `@model TestComponents.Models.testorder` and `@foreach (var items in Model.searchtest) { ... }` –  Jun 28 '17 at 05:22
  • No, Your `searchtest` collection is just `List`, so its just `@items`. And you have not told us what `searchtestid` is - but its a different property of `testorder` (not a property of `searchtest`) –  Jun 28 '17 at 05:29
  • What is the actual value you are returning in " to.searchtest" i think its "testname" – Laxman Gite Jun 28 '17 at 05:36
  • yess,its working now but how should i display in same foreach loop the testid – Mark Antony Jun 28 '17 at 05:38
  • Check below code now – Laxman Gite Jun 28 '17 at 05:40
  • @items is working fine.but i should add another column for testid like @items , but it is possible to retrieve that data ? if yes how ? – Mark Antony Jun 28 '17 at 05:40
  • yah its possible you have to add new item in your collection – Laxman Gite Jun 28 '17 at 05:41
  • how to do that above . i have a seperate function for searchtestid , and how can i modify the code now ? – Mark Antony Jun 28 '17 at 05:43
  • @LaxmanGite can u tel me ? how to do that – Mark Antony Jun 28 '17 at 05:48

2 Answers2

1

Your Return model is not collection so that Your View Should be :

I have added new model with controller action and method check below code it will work for you.

Here was the problem in your code so I have modify and added into collection collection : to.searchtest =dh.searchtes('S', to);

I have modify this model to create below model because no need of this List< string > element now :

    public partial class ItemModel
    {
        public virtual List<string> itemId { get; set; }
        public virtual List<string> itemName { get; set; }

    }

**So your new model should be :** 

        public partial class ItemModel
        {
            public virtual string itemId { get; set; }
            public virtual string itemName { get; set; }

        }

        And Your **Controller Action** should be :

        public ActionResult test()
        {
            ItemModel model = new ItemModel();
            List<ItemModel> itemList = new List<ItemModel>();
            testorder to = new testorder();
            DbHandle dh = new DbHandle();

            var itemmodel = dh.searchtes('S', to);

            //Second way if not work first way
            foreach (var itemvalue in itemmodel)
            {
                model.itemId = itemvalue.itemId;
                model.itemName = itemvalue.itemName;
                itemList.Add(model);
            }


            return View("test", itemList);
        }

        //your method should be 
        public List<ItemModel> searchtes(char flag, testorder to)
        {
            List<string> items = new List<string>();
            connection();
            SqlCommand cmd = new SqlCommand("SPNAME", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PstrOperationFlag", flag);
            cmd.Parameters.AddWithValue("@Pstrtestname", 'w');
            con.Open();
            SqlDataAdapter sd = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sd.Fill(dt);

            ItemModel item = new ItemModel();
            List<ItemModel> itemList = new List<ItemModel>();

            //item.itemId.Add(sdr["testId"].ToString());
            //item.itemName.Add(sdr["testname"].ToString());
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    item.itemId = sdr["testId"].ToString();
                    item.itemName = sdr["testname"].ToString();
                    itemList.Add(item);

                }
            }
            con.Close();
            return itemList;
        }

And here we go for View Page :

@model IEnumerable<TestComponents.Models.ItemModel>  
<!DOCTYPE html>
<html>
<head>

</head>
<body>   
    <table id="myTable">
        <tr class="header">
            <th style="width:60%;">TEST NAME</th>
            <th style="width:40%;">TEST ID</th>
        </tr>

@foreach (var items in Model)
{
        <tr> 
            <td>@items.itemId</td>
            <td>@items.itemName</td>
        </tr>
}
    </table>

Definitely this will help you !!

Cheers!!

Laxman Gite
  • 2,248
  • 2
  • 15
  • 23
0

Your view is accepting the type IEnumerable<TestComponents.Models.testorder> and in your controller you are returning the TestComponents.Models.testorder. so its a main issue.

Another thing : i can see that you want to display searchtest and searchtestid in a table. and from your code i can assume that your model (testorder) would be looking something like following

public class testorder
    {
        public List<string> searchtest { get; set; }
        public List<string> searchtestid { get; set; }
    }

So, you can convert your model something as follow

public class testorder
    {
        public string searchtest { get; set; }
        public string searchtestid { get; set; }
    }

And then your Datalayer can be as follow :

 public List<testorder> searchtes(char flag)
    {
        List<testorder> items = new List<testorder>;
        connection();
        SqlCommand cmd = new SqlCommand("SPNAME", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@PstrOperationFlag", flag);
        cmd.Parameters.AddWithValue("@Pstrtestname", 'w');
        con.Open();
        SqlDataAdapter sd = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sd.Fill(dt);
        using (SqlDataReader sdr = cmd.ExecuteReader())
        {
            testorder to = new testorder();
            while (sdr.Read())
            {
                to = new testorder();
                to.searchtest = sdr["testname"].ToString();
                to.searchtestid = sdr["testid"].ToString();
                items.Add(to);

            }
        }
        con.Close();
        return items;
    }

And finally your controller should render your model something as following :

public ActionResult test()
    {
        List<testorder> to = new List<testorder>();
        DbHandle dh = new DbHandle();
        to = dh.searchtes('S');        
        return View("test", to);
    }

Hope, this helps you.