0

I m working with asp.net with MVC code-first approaches.

I m learning MVC

first, I create a

  • database and
  • then create model class and then
  • context class
  • controller method

record successfully created in the database but I want to get detail of the record but get an error object reference not set to an object

when I click details then give an error object reference not set to an object??

ContextStudent.cs //context class

using System.Data.Entity;

namespace MvcPracTech
{
    public class ContextStudent:DbContext
    {
        public ContextStudent():base("conn")
        {

        }

        public DbSet<students> stud { get; set; }
    }

HomeController.cs


        [HttpGet]                        //reload the page and get data
        public ActionResult create()
        {
            return View();
        }

        [HttpPost]                         //send the data to second action method      
        public ActionResult create(students stud)
        {
            context.stud.Add(stud);
            context.SaveChanges();
            return View();
        }
        [HttpGet]                   //here i m write get method because i want to get the data
        public ActionResult Details(int id)
        {
            var details = context.stud.Find(id);
            return View();


        }

details.cshtml

Details view:

<h2>Details</h2>

<div>
    <h4>students</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.name)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.address)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.address)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.id }) |       //here get error  object referance not set an objectws
    @Html.ActionLink("Back to List", "Index")
</p>

please, help??

what I m missing in above programme??

  • Make sure your `details` object is not `null`. It sounds like `context.stud.Find` is returning `null`. – Matt U Dec 01 '19 at 15:30
  • 1
    `return View();` does not pass any model data to the view. You need to do `return View(details);` – Chetan Dec 01 '19 at 16:32
  • @ChetanRanpariya thanks solve the problem ```return View(details);``` – Ran nusarat Dec 02 '19 at 03:50
  • @ChetanRanpariya can u help me more I m stuck in another question??How do I display a record of database in asp.net mvc? – Ran nusarat Dec 05 '19 at 13:02
  • What issue you are facing while using DisplayFor? – Chetan Dec 05 '19 at 13:20
  • @ChetanRanpariya I want to get the data from the database and display it on the browser?? still, I solving the problem ques:https://stackoverflow.com/questions/59194227/how-do-i-display-a-record-of-database-in-asp-net-mvc – Ran nusarat Dec 05 '19 at 13:39
  • https://stackoverflow.com/questions/25279547/how-to-display-database-records-in-asp-net-mvc-view and https://www.aspdotnet-suresh.com/2016/10/aspnet-mvc-get-display-data-from-database-using-adonet-with-example.html?m=1 – Chetan Dec 05 '19 at 13:44
  • @ChetanRanpariya I update my action method but compile time get an error? – Ran nusarat Dec 05 '19 at 15:32
  • If you have already solved the original problem which you posted the question for, then you should ask different question for the other issue you are facing. – Chetan Dec 05 '19 at 15:35

1 Answers1

0

In the line where you are getting the error, try changing "Model.id" to "model.id"

Jay Ouzts
  • 1
  • 1