15

I'm new to MVC and I'm creating a Registration form for my app but my button click is not working current code is not given below

view

<fieldset>
            <legend>Sign Up</legend>
            <table>
                <tr>
                    <td>
                        @Html.Label("User Name")
                    </td>
                    <td>
                        @Html.TextBoxFor(account => account.Username)
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.Label("Email")
                    </td>
                    <td>
                        @Html.TextBoxFor(account => account.Email)
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.Label("Password")
                    </td>
                    <td>
                        @Html.TextBoxFor(account => account.Password)
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.Label("Confirm Password")
                    </td>
                    <td>
                        @Html.Password("txtPassword")
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" name="btnSubmit" value="Sign Up" />
                    </td>
                </tr>
            </table>
        </fieldset>

model

public class Account
{
    public string Username { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }      

}

controller(not fully completed)

 public class AccountController : Controller
    {
        //
        // GET: /Account/

        public ActionResult Index()
        {
            return View();
        }

        // GET: /Account/SignUp

        public ActionResult SignUp()
        {

            return View();

        }

        [HttpPost]
        public ActionResult SignUp(string userName,string email,string password)
        {
            Account createAccount = new Account();

            createAccount.Username = userName;
            createAccount.Email = email;
            createAccount.Password = password;

            return View("Index");

        }

    }

how to define click event here I tried the http post but its not working I know my code is not correct please point what is the error here

2
  • do you have a form around your view as i cant see one Commented May 8, 2013 at 12:13
  • Check here to do it at Client side Commented May 8, 2013 at 12:38

4 Answers 4

36

ASP.NET MVC doesn't work on events like ASP classic; there's no "button click event". Your controller methods correspond to requests sent to the server.

Instead, you need to wrap that form in code something like this:

@using (Html.BeginForm("SignUp", "Account", FormMethod.Post))
{
    <!-- form goes here -->

    <input type="submit" value="Sign Up" />
}

This will set up a form, and then your submit input will trigger a POST, which will hit your SignUp() method, assuming your routes are properly set up (the defaults should work).

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

1 Comment

It's worth noting for newcomers that this block of code would reside in the View (.cshtml) file
12

as per @anaximander s answer but your signup action should look more like

    [HttpPost]
    public ActionResult SignUp(Account account)
    {
        if(ModelState.IsValid){
            //do something with account
            return RedirectToAction("Index"); 
        }
        return View("SignUp");
    }

Comments

3

yo can try this code

@using (Html.BeginForm("SignUp", "Account", FormMethod.Post)){<fieldset>
    <legend>Sign Up</legend>
    <table>
        <tr>
            <td>
                @Html.Label("User Name")
            </td>
            <td>
                @Html.TextBoxFor(account => account.Username)
            </td>
        </tr>
        <tr>
            <td>
                @Html.Label("Email")
            </td>
            <td>
                @Html.TextBoxFor(account => account.Email)
            </td>
        </tr>
        <tr>
            <td>
                @Html.Label("Password")
            </td>
            <td>
                @Html.TextBoxFor(account => account.Password)
            </td>
        </tr>
        <tr>
            <td>
                @Html.Label("Confirm Password")
            </td>
            <td>
                @Html.Password("txtPassword")
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="btnSubmit" value="Sign Up" />
            </td>
        </tr>
    </table>
</fieldset>}

Comments

0

MVC doesn't do events. Just put a form and submit button on the page and the method decorated with the HttpPost attribute will process that request.

You might want to read a tutorial or two on how to create views, forms and controllers.

2 Comments

<input type="submit" name="btnSubmit" value="Sign Up" /> but its not working for me
Not working is not an error. Explain (in your question, not in comments) what you see and what you expect.

Your Answer

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