Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I try to listing int value from 1 till 30 but until now its not work here is my controller

C#
var lis = new List<int>();
            lis.AddRange(Enumerable.Range(1, 30));
            ViewBag.listku = Enumerable.Range(1, 30).ToString();
            var lispeserta = from item in db.Klaim
                             select item;
            if(!string.IsNullOrEmpty(listku))
            {
                lispeserta = lispeserta.Where(x => x.NoPeserta == listku);
            }


            return View(lispeserta);


and here is my view

C#
@using (Html.BeginForm("Index", "Klaim", FormMethod.Get))
 { 
         <title>search</title> 
    @Html.DropDownList("listku","Semua") <input type="submit" value="Filter" />
}


the error message says "The ViewData item that has the key 'listku' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'."
how should I do?
Posted
Comments
PIEBALDconsult 21-Aug-15 13:12pm    
Enumerable.Range(1, 30).ToString() -- returns a string (and just one string), just as you asked it to.
Use the debugger to see the value of ViewBag.listku , I bet it's not what you expect.
Philippe Mori 21-Aug-15 19:59pm    
The error message is clear and say exactly what is the problem. Then looking at the code, it should be trivial to see that the variable is initialized with a string. You have to write appropriate code to have an IEnumerable of SelectListeItem.

1 solution

Well, you could try comparing the same datatypes!
Assuming that ViewBag.listku and listku are the same thing, the value it contains will be the name of the class - which is going to be along the lines of "System.Linq.Enumerable+<rangeiterator>d__1" rather than the numbers you expect.
Probably you want to do something like this:
C#
var listku = Enumerable.Range(1, 30);
var lispeserta = from item in db.Klaim
                 select item;
    lispeserta = lispeserta.Where(x => listku.Contains(x.NoPeserta));
    return View(lispeserta);


[edit]Gah! Markdown again![/edit]
 
Share this answer
 
v2
Comments
Member 11854019 21-Aug-15 19:24pm    
it wouldn't work
OriginalGriff 22-Aug-15 2:31am    
That is a useless error report.
It tells us nothing about when happens, doesn't happen, any messages, errors, compilation problems, exceptions, or anything.

Would you phone the garage and say "it wouldn't work" and put the phone down? Or the doctor?
So tell us!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900