2015-09-25 33 views
1

我是MVC5中的新成員。我有一個名爲「list」的控制器用於列出書籍,另一個名爲「new」的控制器用於創建新記錄。我用[HttpPost]標記了「新」。但是當我運行該應用程序時,它會提供「無法找到資源 - 請求的URL:/ boks/new /」錯誤。在MVC5中使用帶有[HttpPost]標記的類模型無法找到資源

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

-New查看

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

我看過類似的文章,但我也不能到目前爲止找到一個解決方案。

+0

你不必'list'和'new'控制器 - 你有'list'動作和'new'動作的'book'控制器。因爲你有'書籍'控制器,你的網址應該是:'/ book/new' – jjczopek

回答

1

既然你想瀏覽「/書/新建」你不需要[發佈]

這是更好地創造GET操作的動作和術後另一個動作。

當您瀏覽的地址,GET操作的動作會使用,對於術後的行動將提交表單時使用。

此外,在您的列表視圖中,你應該使用一個鏈接到您的新動作:

@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);    
} 
+0

沒有帖子或者得到它,因爲RedirectToAction在列表中添加了一個空記錄。 – Erdem

+0

@ErdemUğurlubaylar問題解決了嗎? –

+0

非常感謝:)解決了我的問題。 – Erdem