0

I don't have much experience with programming and I'm new to MVC.

I want to fetch some data from database with entity framework and print it in the view.

This is my model:

public class Grad
{
    public int ID { get; set; }
    public string Naziv { get; set; }
    public char KoordinataX { get; set; }
    public char KoordinataY { get; set; }
    public int BrojStanovnika { get; set; }
}
public class GradDBContext : DbContext
{
    public DbSet<Grad> Gradovi { get; set; }
}

this is a controller:

private GradDBContext db = new GradDBContext();

    public ActionResult Index()
    {
        List<int> gradoviList = new List<int>();
        foreach (sea.Models.Grad g in db.Gradovi)
        {
            gradoviList.Add(g.ID);
        }
        ViewData["Gradovi"] = new SelectList(gradoviList);
        return View();
    }

and this is a view:

@foreach (var item in ViewData["Gradovi"] as IEnumerable<int>) ---> error appears here as null reference exception
            {
                <p>item</p>
            }

I know that I have to parse data but don't have idea what did I do wrong

1
  • thank you for your answer. As I sad, I'm new to programming and MVC and still not quite familiar with entity framework and that kind of stuff. This is the first time I'm trying to print something from the database. Yes, just trying to display a list of IDs Commented Apr 29, 2016 at 3:32

2 Answers 2

4

The ViewData item with the key "Gradovi" is typeof SelectList, so it would need to be

@foreach (var item in ViewData["Gradovi"] as SelectList)
{
    <p>@item.Value</p> // or @item.Text

However there is no point generating IEnumerable<SelectListItem> (which is what SelectList is) when you do not need it, and you should be passing your model to the view. Your code in the controller should be

public ActionResult Index()
{
    IEnumerable<int> model = db.Gradovi.Select(x => x.ID);
    return View(model);
}

and in the view

@model IEnumerable<int>
@foreach(var item in Model)
{
    <p>@item</p>
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your code can work like you have it, but I am going to modify it a bit and give you some pointers. I am supplying an answer based on what I see in your post, not what I think you want to achieve at a later stage. There are many ways to accomplish a goal, I will select the simplest way that I will normally use:

public ActionResult Index()
{
     // You will have a repository layer for this part
     GradDBContext db = new GradDBContext();

     // Get a list of your items
     List<Grad> gradovis = db.Gradovi.ToList();

     // I never work with view data, I just pass my view model to the view
     // This way you now have more data to display on the screen (if you need more)
     return View(gradovis);
}

And then your view could look like this:

@model List<Project.Model.Grad>

@foreach (var grad in Model)
{
     <p>@grad.ID</p>
}

Comments

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.