I know there are a lot of questions about this, but not in a single one I've seen (and I've been searching for two hours now), did I see what to write in the razor view (.cshtml file) to fire the controller method associated with saving the model object to the database, or how that object is passed.
So I want to make a simple registration page. I created the Users model, created the view associated with the model, named Register.cshtml, and I access the view through the Account controller which has a Register method:
public ActionResult Register()
{
return View();
}
The tutorial I follow uses the default code generated in the View when you create it for the Users model.
<input type="submit" value="Create" class="btn btn-default" />
And states that you should make a method in the Account Controller as follows:
public ActionResult Register(User obj)
{
if (ModelState.IsValid)
{
DatabaseEntities db = new DatabaseEntities();
db.Users.Add(obj);
db.SaveChanges();
}
return View(obj);
}
(where DatabaseEntities is the name of my database)
This doesn't work, because, on running the application, it says that it doesn't know which one of the two methods to fire.
System.Web.Mvc.ActionResult Register() on type Biblioteca.Controllers.AccountController System.Web.Mvc.ActionResult Register(Biblioteca.Models.User) on type Biblioteca.Controllers.AccountController
This is the error I get.
I thought of renaming the second method RegisterPost(User obj), but then I don't know how to call the method with the argument, because in the razor view, I don't know which is the object that gets created on submitting the form.
I could use an @Html.ActionLink(), but I don't know how to send the object. I actually tried adding an onclick method to the submit button, as follows:
onclick="location.href='@Url.Action("RegisterPost", "Account")'"
But after filling the form and clicking the submit button, nothing happens. Nothing gets inserted into the database.
I don't know what to do now. Please help.
[HttpPost]attribute to yourRegister(User obj)method. That will tell MVC action is the correct on