1

when I run my code it seems that my HttpGet method works fine. But when I try to return the value to my HttpPost action it just runs my HttpGet method and then I get an "NullReferenceException" error.

Here is my code.

My Actions in my controller:

[HttpGet]
        public IActionResult AddMovie(int? id)
        {
            List<Movie> movieList = new List<Movie>();
            movieList = dbContext.Movies.ToList();

            AddMovieViewModel viewModel = new AddMovieViewModel()
            {
                movies = movieList,
                customer = dbContext.Customers.Where(s => s.CustomerId == id).FirstOrDefault()
            };

            return View(viewModel);
        }

        [HttpPost]
        public IActionResult AddMovie (int id,int cid)
        {
            Customer customer = dbContext.Customers.Where(s => s.CustomerId == cid).FirstOrDefault();
            Movie movie = dbContext.Movies.Where(s => s.MovieId == id).FirstOrDefault();
            customer.BorrowingMovies.Add(movie);
            customer.BorrowingMovies.Add(movie);
            return View();
        }

And here my view

@model MovieExampleAppDotNetCore.ViewModels.AddMovieViewModel
@{
    ViewData["Title"] = "AddMovie";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>AddMovie</h1>

<label>Add movie for: @Model.customer.Name</label>

<table style="width:100%" class="table table-bordered table-hover">
    <tr>
        <th>Name</th>
    </tr>
        @foreach (var movie in Model.movies)
     {
            <tr>
                <td>@movie.Name</td>
             <td>
                    @Html.ActionLink("Select", "AddMovie", new { id = movie.MovieId, cid = Model.customer.CustomerId })
             </td>
          </tr>
        }
</table>

I hope someone can help me with my problem.

BDL
  • 21,052
  • 22
  • 49
  • 55
remco
  • 73
  • 9
  • A side note: the first method may have GetMovie name because it's a Get method, not a post method. For your problem, have you tried to put a BP on the post method and check if it's hit ? And determine in which line you get the exception – Kaj Mar 06 '20 at 09:53
  • int? id -> your parameter is nullable in the get and you are using it, also add movie should be post since you are trying to persist something – Kristóf Tóth Mar 06 '20 at 09:53
  • @KristófTóth No, the Html.ActionLink has two parameters which should hit the post method, not the Get one ! – Kaj Mar 06 '20 at 09:55
  • @Kaj yes I have and it shows that the HttpGet method is hit twice. – remco Mar 06 '20 at 09:57
  • Model.customer.CustomerId check for null (CustomerId) – Roman Zeleniy Mar 06 '20 at 09:57
  • Please record a network trafic excerpt from develiper console – Kristóf Tóth Mar 06 '20 at 09:58
  • @Remco As I stated before, change your first method name to GetMovie, not AddMovie because it's a GetMethod. It's not posting but Getting. And try to use [Bind] to not get extra unwanted parameters. – Kaj Mar 06 '20 at 09:58
  • 2
    You will need to wrap the View in a form so the data is actually sent as a POST – Ryan Thomas Mar 06 '20 at 10:02
  • 1
    "But when i try to return the value to my HttpPost action it just runs my HttpGet method "... because an ActionLink (which generates a HTML hyperlink) always causes a GET request. If you want to POST, you have to use a form. – ADyson Mar 06 '20 at 10:12

1 Answers1

5

The HTML ActionLink does not work with POST so is hitting your GET method. See this question.

To overcome and resolve your issue, wrap the button in a Form, something like this should do the trick.

@using (Html.BeginForm("AddMovie", "ControllerName", new { id = movie.MovieId, cid = Model.customer.CustomerId }, FormMethod.Post))
{
    <button class="btn btn-primary" type="submit">
        Add Movie
    </button>
}
Ryan Thomas
  • 1,724
  • 2
  • 14
  • 27