Osman Esen
Osman Esen

Reputation: 1696

Displaying a list of strings in ASP.NET

I'm currently making some exercises regarding displaying some tables in ASP.NET application. I Have following model class:

public class DynamoDB
{
    public IEnumerable<string> GetOsman { get; set; }
}

and controller class:

public class DynamoController : Controller
{

    // --- THIS PART IS UPDATED ---
    public ActionResult Index()
    {
        DynamoDB model = new DynamoDB();
        model.GetOsman = LoadAI();
        return View(model);
    }

    public List<string> LoadAI()
    {
        List<string> list = new List<string>();
        list.Add("Testing");
        list.Add("MCV");
        list.Add("Project");
        return list;
    }
}

and finally my Razor file:

@{
ViewBag.Title = "Home Page";
}

@model FinalApplication.Models.DynamoDB

<div class="jumbotron">
    <h1>Home Monitoring Application</h1>
</div>


@foreach (var item in Model.GetOsman)
{
    <text>
        @item
    </text>
}

But it still returns the error "Object reference not set to an instance of an object" at following line:

@foreach (var item in Model.GetOsman)

I made a similar post yesterday, since I got the same error when I was accessing the data from a DynamoDB table, but I'm getting the same error even with this simple example. My solution explorer look like this:

enter image description here

Upvotes: 0

Views: 3108

Answers (3)

Vsevolod Goloviznin
Vsevolod Goloviznin

Reputation: 12324

You should pass your model to the view, not model.GetOsman.

return View(model);

Also, create Dynamo folder under Views and include Index.cshtml there.

Upvotes: 3

fly_ua
fly_ua

Reputation: 1054

Seems your view expects DynamoDB model, but you are passing

 return View(model.GetOsman);

Try passing model to your view

 return View(model);

Upvotes: 0

JLRishe
JLRishe

Reputation: 101662

The object that you're passing to View() is model.GetOsman instead of model.

Do this:

return View(model);

Upvotes: 3

Related Questions