Problem with Html.BeginForm (ASP.NET MVC)

815 Views Asked by At

I have tried to make a form in a cshtml file send parameters to a function called "AddNewUser" inside a Controller I've named "AccountController".

Inside SignUp.cshtml I used the method Html.BeginForm like so:

@using (Html.BeginForm("AddNewUser", "Account", FormMethod.Post)){
}

Inside AccountContrller.cs I have the function AddNewUser whose header is:

 [HttpPost]
        public ActionResult AddNewUser(SignUpViewModel model)

Here is the directory tree, showing the locations of both files: directory tree

enter image description here

And here is the I get when I press send on the form in SignUp.cshtml: Error

enter image description here

3

There are 3 best solutions below

1
David On BEST ANSWER

In the picture of your directory tree you highlight a view called SignUp. But if you read the picture of your error message you'll see that it's looking for a view called AddNewUser. Which matches the name of your controller action:

public ActionResult AddNewUser(SignUpViewModel model)

So presumably your controller action is trying to return a view. And that view doesn't exist.

You'd either need to create that view, or don't return a view from that controller action. You can instead return a redirect to another action. For example, if you want the user to be redirected to SignUp after the form is posted then you'd redirect them:

return RedirectToAction("SignUp");

Alternatively, you can return that view if you rename the controller action:

[HttpPost]
public ActionResult SignUp(SignUpViewModel model)

(And of course update the BeginForm call to reflect the new action method name.)

This is common practice for handling form posts where you might want to re-display the form in the event of an error. Both the GET and the POST action methods can have the same name as long as the method signatures are different. (Which they should be, since the GET action would have no reason to accept a SignUpViewModel.)

That way the POST action can return the view for the user to try again. The general flow for a form POST action would be to return the view to the user in the event of an error, or redirect the user in the event of success.


You might find that you can also specify the view name explicitly when it doesn't match the controller action name:

return View("SignUp");

While this is technically allowed, I highly discourage it unless you really know what you're doing in terms of HTTP actions and routes and RESTfulness, etc. Because the URL in the browser would still be AddNewUser, not SignUp. This can cause other problems and weird bugs down the road.

1
Jayrag Pareek On

you need to set return view url in your action ActionResult AddNewUser try this:

return View("~/Views/Account/SignUp.cshtml");
0
Rui Lionel On

just return the view that you want. in this case, try this:

certify that Signup is at the same controller

return View("SignUp");

Otherwise

return View("~/Views/Account/SignUp.cshtml");