0

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

3
  • if you pass into view the model you have just to iterate to it, @foreach (var item in Model) Commented Dec 10, 2014 at 13:22
  • 2
    Change return View(model.GetOsman); to return View(model); You need to send the complete model. Commented Dec 10, 2014 at 13:22
  • I'm still getting the same error even though I have modified to return View(model); Commented Dec 10, 2014 at 14:28

3 Answers 3

3

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.

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

2 Comments

Yes and I still can't figure out why. From the picture above, I have an Index.cshtml file in both "Home" and "Status" folder. That might be the cause of the issue perhaps ?
Try creating Dynamo folder and putting and Index.cshtml there
3

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

Do this:

return View(model);

Comments

0

Seems your view expects DynamoDB model, but you are passing

 return View(model.GetOsman);

Try passing model to your view

 return View(model);

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.