在MVC5中使用[HttpPost]标记的类模型无法找到资源

时间:2015-09-25 18:48:02

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

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

-Controllers

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

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

1 个答案:

答案 0 :(得分:1)

由于您想要浏览“/ Book / New”,因此您不需要[发布]

最好为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);            
}