在MVC5中,无法找到带有[HttpPost]标记的类模型的资源

本文关键字:HttpPost 模型 资源 MVC5 | 更新日期: 2023-09-27 18:14:30

我是MVC5的新手。我有一个名为"list"的控制器用于列出图书,另一个名为"new"的控制器用于创建新记录。我用[HttpPost]标记了"新"。但是当我运行应用程序时,它给出了"资源无法找到-请求的Url:/books/new/"错误。

控制器

using System.Web.Mvc;
using BookStore.Data;
using BookStore.Models;
namespace BookStore.Controllers
{
    public class BookController : Controller
    {
        // GET: Book
      public  ActionResult List()
        {
            return View(BookData.Books);
        }
        [HttpPost]
        public ActionResult New(Book newBook) 
        {
            BookData.Books.Add(newBook);
            return RedirectToAction("List");
        }
    }
}

模型

namespace BookStore.Models
{
    public class Book
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public int PublishYear { get; set; }
        public decimal price { get; set; }
    }
}
列表视图

-

@model IEnumerable<BookStore.Models.Book>
@{
    ViewBag.Title = "List";
}
<h2>List</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Author)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PublishYear)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.price)
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Author)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PublishYear)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.price)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}
</table>
<<p> -视图/strong>
model BookStore.Models.Book
@{
    ViewBag.Title = "New";
}
<h2>New</h2>
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>Book</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Author, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Author, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Author, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.PublishYear, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PublishYear, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PublishYear, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.price, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.price, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.price, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

-BookData

**using System.Collections.Generic;
using BookStore.Models;
namespace BookStore.Data
{
    public class BookData
    {
        public static List<Book> Books = new List<Book>
        {
            new Book
            {
                Id=1,
                Title="Test1",
                Author="John Doe",
                PublishYear=2015,
                price=10
            },
            new Book
            {
                Id=2,
                Title="Test2",
                Author="Jane Doe",
                PublishYear=2014,
                price=15
            }
        };
    }
}

我读过类似的文章,但是到目前为止我还找不到解决办法。

在MVC5中,无法找到带有[HttpPost]标记的类模型的资源

既然你想浏览"/Book/New"你就不需要[Post]

最好为get操作创建一个操作,为post操作创建另一个操作。

get操作的操作将在浏览该地址时使用,post操作的操作将在提交表单时使用。

同样在你的列表视图中,你应该使用一个链接到你的新操作:

@Html.ActionLink("Create New", "New")

例如:

[HttpGet]
public virtual ActionResult New()
{
    return View();
}
[HttpPost]
public virtual ActionResult New(Book entity)
{
    try
    {
        if (ModelState.IsValid)
        {
            //Save book
            return RedirectToAction("List");
        }
    }
    catch (Exception ex)
    {
        ModelState.AddModelError("", ex.Message);
    }
    return View(entity);            
}