2

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.

1
  • Try adding [HttpPost] attribute to your Register(User obj) method. That will tell MVC action is the correct on Commented Sep 22, 2017 at 14:27

2 Answers 2

3

Add attribute [HttpGet] to the first action and [HttpPost] to the second action:

[HttpGet]
public ActionResult Register()
{
    return View();
}

[HttpPost]
public ActionResult Register(User obj)
{
    if (ModelState.IsValid)
    {
        DatabaseEntities db = new DatabaseEntities();
        db.Users.Add(obj);
        db.SaveChanges();
    }
    return View(obj);
}

You can read more on MSDN

Sign up to request clarification or add additional context in comments.

Comments

2

To expand a bit, try adding [HttpPost] above the second Action method.

[HttpPost]
public ActionResult Register(User obj)
{
    if (ModelState.IsValid)
    {
        DatabaseEntities db = new DatabaseEntities();
        db.Users.Add(obj);
        db.SaveChanges();
    }
    return View(obj);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.