1

I am trying to view list of items from database ( I am using Entity Framework).

My Repository method:

public List<string> getListOfItems(int i)
{
    return (from x in db.Items where x.ID == i select x.Text).ToList();
}

My Controller:

public ActionResult Index()
{
    var itemOutput = repo.getListOfItems(1); // I just put 1 since I didn't know how to specify "i" - However theoretically it should return first item in database but its not
    ViewBag.itemOutput = itemOutput ;

    return View();
}

Model:

public class items
{
    [Key]
    public int ID{ get; set; }
    public string name{ get; set; }
    public string quantity{ get; set; }
}

ItemModel:

public class itemModels
{ 
    public List<List<string>> itemData{ get; set; }
}

View:

@foreach (var item in ViewBag.itemOutput )
{
    <table id="t01">
        <tr>
            <td>@item.name</td>
        </tr>
    </table>
}
10
  • what is issue ? Commented Mar 20, 2018 at 14:16
  • it is not showing the items from the database. technically it should show first item (e.g. Camera) Commented Mar 20, 2018 at 14:17
  • Have you check data is retrive from the database, have you check correct database is pointing? Commented Mar 20, 2018 at 14:18
  • 2
    Why are you not using a proper model here? Don't use the ViewBag unless you absolutely have to. If you don't know how to pass a model to your view, then you should go back to basics and follow some tutorials first. Commented Mar 20, 2018 at 14:24
  • 1
    Yes, 1) don't use ViewBag, 2) do use a ViewModel, 3) GetList(ID) will always return a list of 0 or 1 items. Commented Mar 20, 2018 at 14:26

2 Answers 2

2

Answer:

ViewBag.itemOutput is a List<string> which makes item a string.

Therefore, use @item instead of @item.name (as string does not have .name property) in your view:

@foreach (var item in ViewBag.itemOutput )
{
    <table id="t01">
        <tr>
            <td>@item</td>
        </tr>
    </table>
}

Also, to get the full list, you could do:

public List<string> getListOfItems()
{
    return (from x in db.Items select x.Text).ToList();
}

And then just call getListOfItems() with no param.


Random comments:

1) Don't use plural for class name, unless the class is somewhat a collection of things

--> public class item // without s

2) You said in comments that items are full varchar which is untrue as per your class definition (you have ID, name & quantity).

3) Using string for quantity is a bit weird.

4) You could indeed change your getListOfItems method to:

public List<item> getListOfItems()
{
    return (from x in db.Items select x).ToList();
    // which can be simplified to:
    // return db.Items.ToList();
}

but you should then change your view back to @item.name.

This would however allow you to do:

@foreach (var item in ViewBag.itemOutput )
{
    <table id="t01">
        <tr>
            <td>@item.name</td>
            <td>@item.quantity</td>
        </tr>
    </table>
}

5) You have an ItemModel but you are not using it. You could modify it and use it instead of the ViewBag.

Sign up to request clarification or add additional context in comments.

5 Comments

Great! :) it worked but at the moment in my controller, I've put var itemOutput = repo.getListOfItems(1); so its returning first item how can I instead loop through my items in my database table??
from x in db.Items where x.ID == i will return one item with the ID == 1. You are asking for a single item and returning it in a list.
@Warda I edited my answer to add the get the full list part
Perfect! Thank you so much! :)))
@Warda glad it helped you :) I edited my answer to add some comments
0

getListOfItems() returns a list of string, but you're referring to an actual object in ViewBag.itemOutput

Instead of select x.Text do a select x, and make the return value List<items>

public List<items> getListOfItems(int i)
{
    return (from x in db.Items where x.ID == i select x).ToList();
}

Then you can keep your razor template the same to refer to @item.name

3 Comments

yeah the items in my database is varchar - all strings. so what do you suggest??
What I'm saying is your Razor template is trying to reference a full item, but you're only returning back strings from the repository.
@int21h is correct. Your action method and the view do not match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.