【问题标题】:The Resource Cannot be Found with a class model tagged with [HttpPost] in MVC5在 MVC5 中用 [HttpPost] 标记的类模型找不到资源
【发布时间】:2015-09-26 03:01:11
【问题描述】:

我是 MVC5 的新手。我有一个名为“list”的控制器用于列出书籍,另一个名为“new”的控制器用于创建新记录。我用 [HttpPost] 标记了“新”。但是当我运行应用程序时,它给出了“找不到资源 - 请求的 URL:/boks/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>

-新视图

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
            }
        };
    }
}

我已经阅读了类似的文章,但到目前为止我无法找到解决方案。

【问题讨论】:

  • 你没有 listnew 控制器 - 你有 book 控制器和 list 动作和 new 动作。因为你有book控制器,你的url应该是:/book/new

标签: c# asp.net-mvc asp.net-mvc-5


【解决方案1】:

既然要浏览“/Book/New”,就不需要[Post]

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

浏览该地址时会使用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);            
}

【讨论】:

  • 由于 RedirectToAction,没有 post 或 get 它会在列表中添加一个空记录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
相关资源
最近更新 更多