i'm new in mvc5. have controller named "list" listing books , controller named "new" creating new record. tagged "new" [httppost]. when run application, gives "the resource cannot found - requested url:/boks/new/" error.
-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"); } } }
-model
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; } } }
-list view
@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 view
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 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 } }; } }
i have read similar articles ı can't able find solution far.
since want browse "/book/new" don't need [post]
it's better create action operation , action post operation.
the action operation use when browse address, action post operation use when submitting form.
also in list view should use link new action:
@html.actionlink("create new", "new")
as example:
[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); }
Comments
Post a Comment