[c#] How to trigger button click in MVC 4

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

This question is related to c# asp.net-mvc-4

The answer is


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.


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>}

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");
    }