Saturday, August 17, 2013

TextBox not displayed when rendering form with Html.BeginForm

TextBox not displayed when rendering form with Html.BeginForm

I've just begun learning ASP MVC 4, and I'm working on a basic exercise,
which is a book hosting website.
I'm currently working on a controller for adding a new book to the
repository. The view for the appropriate action is strongly-typed to the
Book class as its model. Book is a very simple model consisting of title,
author and so forth.
My AddBook controller currently looks like this: (I haven't implemented
any database insertion logic on POST, yet)
public class AddBookController : Controller
{
[HttpGet]
public ActionResult AddBook()
{
return View();
}
[HttpPost]
public ActionResult AddBook(Book book)
{
return View();
}
}
My view is also very simple:

@model Bookshare.Models.Book
@{
ViewBag.Title = "AddBook";
}
Add a new book
@using (Html.BeginForm())
{
Html.TextBoxFor(model => model.Title);
Html.TextBoxFor(model => model.Author);
Html.TextBoxFor(model => model.PublishingCompany);
Html.TextBoxFor(model => model.ReleaseYear);
Html.TextBoxFor(model => model.Summary);
}
And yet, when I call this action, all I can see is the "Add a new book"
header and the submit button for the form. No text boxes whatsoever. This
also happens if I use the plain old Html.TextBox syntax. Viewing the
page's source reveals just an empty form tag.
What am I doing wrong here?

No comments:

Post a Comment