【发布时间】: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
}
};
}
}
我已经阅读了类似的文章,但到目前为止我无法找到解决方案。
【问题讨论】:
-
你没有
list和new控制器 - 你有book控制器和list动作和new动作。因为你有book控制器,你的url应该是:/book/new
标签: c# asp.net-mvc asp.net-mvc-5