Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9001315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:25:12+00:00 2026-06-16T00:25:12+00:00

I have this basic error that i can’t fix… Object reference not set to

  • 0

I have this basic error that i can’t fix… Object reference not set to an instance of an object

I’m using asp.net mvc4 with ef

My controller

 public class PostController : Controller
    {
        private UsersContext db = new UsersContext();

        public ActionResult Index()
        {
            return View(db.Posts.ToList());
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection values)
        {
            var post = new Post();
            TryUpdateModel(post);

            if(ModelState.IsValid)
            {
                var context = new UsersContext();
                var username = User.Identity.Name;
                var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
                var userid = user.UserId;
                // var firstname = user.FirstName;

                post.UserId = userid;
                post.Date = DateTime.Now;

                db.Posts.Add(post);
                db.SaveChanges();
            }
            return View("Index");
        }

and my view :

@model IEnumerable<MyProject.Models.Post>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Content)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Date)
        </th>
        <th></th>
    </tr>

   @foreach (var item in Model) { //**line with error**

    <tr>
        <td>
                @Html.DisplayFor(modelItem => item.Content)
        </td>
        <td>
                @Html.DisplayFor(modelItem => item.Date)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.PostId }) |
            @Html.ActionLink("Details", "Details", new { id=item.PostId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.PostId })
        </td>
    </tr>
}
</table>

Thanks for your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:25:13+00:00Added an answer on June 16, 2026 at 12:25 am

    You forgot to send a model to the view after the GET action in your controller. Your return statement should probably be:

        public ActionResult Create()
        {
            return View("Create", new Post());
        }
    

    You have to supply something for the model or it will be null in the view.

    You’ll probably have the same problem with your POST method as well, which returns:

    return View("Index");
    

    Rather than doing that, you should probably just redirect to the Index action:

    return RedirectToAction("Index");
    

    Also, you would only want to do that on success – if the model state wasn’t valid, you probably want to go back to the create screen to show the errors. So your post action would end up something like this:

       [HttpPost]
        public ActionResult Create(FormCollection values)
        {
            var post = new Post();
            TryUpdateModel(post);
    
            if(ModelState.IsValid)
            {
                var context = new UsersContext();
                var username = User.Identity.Name;
                var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
                var userid = user.UserId;
                // var firstname = user.FirstName;
    
                post.UserId = userid;
                post.Date = DateTime.Now;
    
                db.Posts.Add(post);
                db.SaveChanges();
    
                return RedirectToAction("Index");
            }
    
            return View("Create", post);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this page that has some basic custom tabs: http://demo.unlockedmanagement.com/users/view/2 * NOTE
I have this table, which can be seen as a basic custom gantt chart:
I have this basic script that makes an element show onmouseenter, and hide onmouseleave.
I have this basic code that is failing: ftpconfighash = open(#{RAILS_ROOT}/config/ftp.yml) {|f| YAML.load(f) }
Hi I have this basic random quote script, but I would like it to
I have a js.erb with this basic content: $(#currencies).html(<%= options_for_select([['Dollar', '$']])%>); And the html
Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
i know this is basic but somehow i have been stuck here for some
I realize this is a basic question but I have searched online, been to
I'm a noob so this is probably basic stuff, but I have tried to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.