skip to Main Content

I’m getting the following error:

System.NullReferenceException: ‘Object reference not set to an instance of an object.’
Microsoft.AspNetCore.Mvc.Razor.RazorPage.Model.get returned null.

I’m trying to pass an Id from a view to a controller HttpPost action method.

Here is my code:

Controller:

public class HomeController : Controller
{
        ...

        [Authorize]
        public IActionResult List()
        {
            var currentUserId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var currentCars = db.Cars.Where(x => x.CreatedByUserId == currentUserId)
                .Select( x => new CarsListViewModel
                 {
                   CarId = x.Id,
                   CreatedOn = x.CreatedOn,
                   CreatedByUserId = x.CreatedByUserId,
                   CreatedByUserName = x.CreatedByUserName,
                   Firstname = x.PrimaryData.Firstname,
                   Lastname = x.PrimaryData.Lastname
                  }).
                ToList();

            return View(currentCars);
        }


        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public IActionResult List(int carId)
        {
            var Car = db.Cars.FirstOrDefault(x => x.Id == carId);

                db.Cars.Remove(Car);

                db.SaveChanges();

                return View();
        }

ViewModel:

public class CarListViewModel
    {
        public int CarId { get; set; }

        public DateTime CreatedOn { get; set; }

        public string CreatedByUserId { get; set; }

        public string CreatedByUserName { get; set; }

        public string Firstname { get; set; }

        public string Lastname { get; set; }
    }
}

View (List.cshtml):

@model List<CVBuilder.ViewModels.CarListViewModel>
@{
    ViewData["Title"] = "List of current cars";
}

<div class="col-md-10 offset-md-1">
    <table class="table table-hover text-nowrap">
        <thead>

            ...

        </thead>
        <tbody>
            @for (int i = 0; i < Model.Count; i++)
            {
            <tr>
                <td>@Model[i].CreatedOn</td>
                <td>@Model[i].CreatedByUserName</td>
                <td>@Model[i].Firstname</td>
                <td>@Model[i].Lastname</td>
                <td>

                    <form method="post">
                        <input type="hidden" name="carId" value="@Model[i].CarId" />
                        <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                    </form>

                </td>
            </tr>
            }
        </tbody>
    </table>

    @if (Model.Count == 0)
    {
    <div class="text-center"><p>No cars created.</p></div>
    }

</div>

2

Answers


  1. try it:

         public IActionResult List([FromForm]int carId){
    
         //  return View(); remove
         return RedirectToAction("List")
        }
    
    Login or Signup to reply.
  2. You can try this to pass a particular model id from view to controller in MVC

    <button href="@Url.Action("List", "ControllerName", new { carId = @Model[i].CarId},null)" button>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search