I am beginner in asp.net mvc . I am going to connect an action of button in view.
but i cant. i work with web form, in fact i want to click on the button, create action will be called and insert the data.
my code is as following:
The Controller:
namespace BookStore.Controllers
{
public class BookController : Controller
{
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(book bookobj)
{
var dBook=new DBook();
dBook.Insertbook(bookobj);
return RedirectToAction("Index", "Home");
}
}
}
The View:
@model BookStore.Models.DomainObject.book
@{
ViewBag.Title = "Create";
}
<h2>insert data/h2>
@using(Html.BeginForm())
{
@Html.ValidationSummary(true);
<fieldset>
<div>
@Html.LabelFor(model=> model.book_name)
</div>
<div>
@Html.EditorFor(model => model.book_name)
</div>
<div>
@Html.LabelFor(model=>model.book_qty)
</div>
<div>
@Html.EditorFor(model=>model.book_qty)
</div>
<br/>
<div>
<input id="Button_craete_book" type="button" value="insert" />
</div>
</fieldset>
}
Change the button type to “submit”:
<input id="Button_craete_book" type="submit" value="insert" />That will post the form and values to the Edit method in the controller, as marked with the HttpPost attribute.