RedirectToAction()方法未返回302状态代码

时间:2019-03-18 16:48:34

标签: asp.net-mvc

我正在尝试使用RedirectToAction()方法从一个Action重定向到另一个Action。但是,执行代码时,状态代码中显示的是“ 200 OK”,而不是“找到的302”。 Screen Shot

基本上,“我的保存”操作应重定向到“索引操作”,但不重定向。

在“电影”控制器中保存动作

     [ValidateAntiForgeryToken]
    [HttpPost]
    public ActionResult Save(Movies movies)
    {


        if (!ModelState.IsValid)
        {
            var viewModel = new MovieViewModel
            {
                Generes = _context.Generes.ToList()
            };

            return View("MovieForm", viewModel);
        }

        if (movies.ID == 0)
        {
            movies.DateAdded = DateTime.Now;
            _context.Movies.Add(movies);
        }

        else
        {
            var MoviesInDb = _context.Movies.Single(m => m.ID == movies.ID);

            MoviesInDb.MovieName = movies.MovieName;
            MoviesInDb.Releasedate = movies.Releasedate;
            MoviesInDb.GenereID = movies.GenereID;
            MoviesInDb.NumberInStock = movies.NumberInStock;
        }
        _context.SaveChanges();
        return RedirectToAction("Index","Movies");
    }

电影控制器中的索引操作

 public ActionResult Index()
    {
        var Movies = _context.Movies.Include(m => m.Genere).ToList();
        return View(Movies);
    }

索引视图

@model IEnumerable<Vidly.Models.Movies>
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<title> Movies</title>
<h2>Movies</h2>

<button class="btn btn-primary"> New Movie </button>
<table class="table table-striped" >
    <thead>
        <tr>
            <th> Movies </th>
            <th> Category </th>

        </tr>
    </thead>

    <br/>
    <tbody>

        @foreach (var Movie in Model)
        {

            <tr>

                <td>
                    @Html.ActionLink(Movie.MovieName, "Edit", "Movies", new { id = Movie.ID },null)
                </td>
                <td>
                    @Movie.Genere.Name
                </td>

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

提供“保存”操作的视图是MovieForm.cshtml

  @model Vidly.ViewModels.MovieViewModel
@{
    ViewBag.Title = "MovieForm";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Movie Form</h2>
@using (Html.BeginForm("Save", "Movies"))
{

    <div class="form-group">
        @Html.LabelFor(m => m.Movies.MovieName)
        @Html.TextBoxFor(m => m.Movies.MovieName, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Movies.MovieName)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Movies.Releasedate)
        @Html.TextBoxFor(m => m.Movies.Releasedate, "{0:MM/dd/yyyy}", new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Movies.Releasedate)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Movies.GenereID)
        @Html.DropDownListFor(m => m.Movies.GenereID, new SelectList(Model.Generes, "ID", "Name"), "Select the Genere", new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Movies.GenereID)
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Movies.NumberInStock)
        @Html.TextBoxFor(m => m.Movies.NumberInStock, new { @class = "form-control" })
        @Html.ValidationMessageFor(m => m.Movies.NumberInStock)
    </div>

    @Html.HiddenFor(m => m.Movies.ID)
    @Html.AntiForgeryToken()
    <br />
    <button type="submit" class="btn btn-primary"> Submit</button>

}

有什么想法为什么要返回200 Ok状态代码?

1 个答案:

答案 0 :(得分:1)

也许ModelState无效

...
if (!ModelState.IsValid)
{
  var viewModel = new MovieViewModel
  {
    Generes = _context.Generes.ToList()
  };

  return View("MovieForm", viewModel);
}
...