whoah
whoah

Reputation: 4443

mvc4 input return to action empty string

I have a problem. Value from input is empty in action method, but value from submit button is ok. Here is my code:

    @using (Html.BeginForm("ActivateUser", "Account", new { ReturnUrl = ViewBag.ReturnUrl }))
    {
            foreach (var users in Model)
            { 
                <li>
                    @users.name<br />
                    <input type="text" id="Bounty" name="Bounty" /><br />
                <button type="submit" name="userId" value="@users.userId" title="go">Go!</button><br /><br />
                </li>
            }

and my action code:

    [HttpPost]
    public ActionResult ActivateUser(string Bounty, string userId)...

This is strange, because userId has got a value, but Bounty is empty ("" value).. How can I resolve it?

Regards!

Upvotes: 0

Views: 206

Answers (1)

technivore
technivore

Reputation: 136

Make a separate form for each user. Right now the page is submitting all of the inputs at once and the last one "wins", and it's probably empty.

foreach (var users in Model)
{ 
    @using (Html.BeginForm("ActivateUser", "Account", new { ReturnUrl = ViewBag.ReturnUrl }))
    {
        <li> @users.name<br />
            <input type="text" name="Bounty" /><br />
        <button type="submit" name="userId" value="@users.userId" title="go">Go!</button><br /><br />
        </li>
    }
}

Upvotes: 2

Related Questions