C# 未调用HttpPost方法

C# 未调用HttpPost方法,c#,asp.net-core-mvc,C#,Asp.net Core Mvc,当我运行代码时,我的HttpGet方法似乎工作得很好。但是,当我尝试将该值返回到HttpPost操作时,它只运行HttpGet方法,然后出现“NullReferenceException”错误 这是我的密码 我在控制器中的操作: [HttpGet] public IActionResult AddMovie(int? id) { List<Movie> movieList = new List<Movie>();

当我运行代码时,我的HttpGet方法似乎工作得很好。但是,当我尝试将该值返回到HttpPost操作时,它只运行HttpGet方法,然后出现“NullReferenceException”错误

这是我的密码

我在控制器中的操作:

[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();
        }
[HttpGet]
公共IActionResult AddMovie(int?id)
{
List movieList=新列表();
movieList=dbContext.Movies.ToList();
AddMovieViewModel viewModel=新的AddMovieViewModel()
{
电影=电影演员,
customer=dbContext.Customers.Where(s=>s.CustomerId==id).FirstOrDefault()
};
返回视图(viewModel);
}
[HttpPost]
公共IActionResult AddMovie(内部id,内部cid)
{
Customer=dbContext.Customers.Where(s=>s.CustomerId==cid.FirstOrDefault();
Movie Movie=dbContext.Movies.Where(s=>s.MovieId==id).FirstOrDefault();
customer.借入电影.添加(电影);
customer.借入电影.添加(电影);
返回视图();
}
这是我的看法

@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>
@model MovieExampleAppDotNetCore.ViewModels.AddMovieViewModel
@{
ViewData[“Title”]=“AddMovie”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
AddMovie
为@Model.customer.Name添加电影
名称
@foreach(Model.movies中的var movie)
{
@电影,名字
@ActionLink(“选择”,“添加电影”,新建{id=movie.MovieId,cid=Model.customer.CustomerId})
}

我希望有人能帮我解决这个问题。

HTML ActionLink与POST不兼容,因此正在使用GET方法。看

要克服并解决您的问题,请将按钮包装成一个表单,类似这样的东西应该可以解决问题

@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>
}
@使用(Html.BeginForm(“AddMovie”,“ControllerName”,new{id=movie.MovieId,cid=Model.customer.CustomerId},FormMethod.Post))
{
添加电影
}

A旁注:第一个方法可能有GetMovie名称,因为它是Get方法,而不是post方法。对于您的问题,您是否尝试在post方法上添加BP并检查是否命中?确定你在哪一行得到例外?id->您的参数在get中可以为空,并且您正在使用它,而且添加电影应该是post,因为您正在尝试持久化something@KristófTóth不,Html.ActionLink有两个参数,应该命中post方法,而不是Get方法!您需要将视图包装成一个表单,以便数据实际上作为POST发送,“但是当我尝试将值返回到我的HttpPost操作时,它只运行我的HttpGet方法”。。。因为ActionLink(生成HTML超链接)总是导致GET请求。如果你想发布,你必须使用表单。