4

i'm working on ASP.NET MVC project and every time i tried to run my view Register.cshtml i get this error :

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /User/Register

i'm trying to develop a Registration page with a view code :

  @{
        ViewBag.Title = "Register";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

<h2>Register</h2>

<form action='@Url.Action("Register", "Controller")'>

    <input type="hidden" name="FormType" value="A" />
    <input type="hidden" name="FormType" value="B" />
    <input type="text" name="Name" placeholder="enter your name" />
    <input type="text" name="Password" placeholder="enter your name" />
    <input type="radio" name="typeOfForm" class="radioBtn" value="A">Form A
    <input type="radio" name="typeOfForm" class="radioBtn" value="B">Form B
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <div style="display: none" id="formA" action="/actionA">
        <input type="text" name="City" placeholder="City" />  <input type="submit" value="Register Me!" />
    </div>

    <div style="display: none" id="formB" action="/actionB">
        <input type="text" name="Age" placeholder="Age" /><input type="submit" value="Register Me!" />
    </div></form>

<script>
    $('.radioBtn').click(function () {
        var formType = $(this).val();
        //alert(formType);
        if (formType == "A") {
            $('#FormA').show();
            $('#FormB').hide();
        } else {
            $('#FormB').show();
            $('#FormA').hide();
        }
    });</script>

with a UserController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BootstrapSite4.Controllers
{
    public class UserController : Controller
    {   [HttpGet]
        [HttpPost]
        // GET: User
        public ActionResult Register(char FormType, string name, string password)
        {
            Seller S = new Seller();
            DeliveryMan D = new DeliveryMan();
            if (FormType=='A')
                S.Name = name;
            else 
                D.Name = name;
            return View();}}}

i tried to change my RegisterRoutes but still getting the same error .. i think my error with the location only ! just a briefly description about the registration idea : i have tow types of users that will fill the registration form and depending on what they have chosen(in radio buttons) the rest of the form will appear in the same page to let them continue registration then add it to the right database .

2
  • What routes do you have defined? Commented Jul 20, 2015 at 12:51
  • i have change the RegisterRoutes from controller = "Home", action = "Index" to controller = "User", action = "Register" Commented Jul 20, 2015 at 13:05

1 Answer 1

4

You need to add an HttpGet Register method to the UserController as well:

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

Remember that you need 2 Register methods:

  1. GET - Requests data from a resource - in this case requests to go to the Register view.
  2. POST - Submitting data - in this case sends users info to the server, to register the user.

More reading on Http Get and Post

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

8 Comments

@user5067119, can you update the controller code with the change please?
Thanks. You've added HttpGet to the same method. You need to create a new Register method (so you will have 2). Keep your original as is, then add the one I've indicated in my answer below it.
OP, make sure you understand the difference between the HTTP verbs and how they work. They are important.
thanks a lot it's working !! but whenever i press the "Register Me!" button i get the same error !
@user5067119, great. Your form is submitting to the wrong controller. Change @Url.Action("Register", "Controller") to @Url.Action("Register", "User")
|

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.