Question

I realise this is extremely elemetry but I would like to pass a text box value to a controller method when using Html.BeginForm

this is the what I tried so far

cshtml

@using (Html.BeginForm("GetPassword", "Home"))
{
    <div id="panEmailForgotPassword">
        <h3 style="margin: 0;">
            Please enter your email address</h3>
        <br />
        <table style="width: 57%; padding-bottom: 10px; height: 126px;">
            <tr>
                <td>
                    <label style="width: 110px;">
                        Email address:</label>
                </td>
                <td>
                    @Html.TextBoxFor(m => m.EmailForgotPassword, new { name = "txtEmailForgotPassword" })
                </td>
            </tr>

controller method

[HttpPost]
    public ActionResult GetPassword(string txtEmailForgotPassword)
    {
        ViewModel model = new ViewModel();

        model.VerifyUserExists(txtEmailForgotPassword);
        return View(model);
    }

txtEmailForgotPassword returns a null value.

How would I do this correctly?

Arian

Was it helpful?

Solution

I dont think there is need to pass single string paramater, rather you can pass your whole model from view

[HttpPost]
    public ActionResult GetPassword(YourModel yourmodel)
    {
        ViewModel model = new ViewModel();

        model.VerifyUserExists(yourmodel.EmailForgotPassword);
        return View(model);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top