0

When I click on the link "Foo", I hit the break point in the LogOn action but username is always null. I don't understand why.

<a class="jqselect" id="myId" href="#">Foo</a>    
@using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { id = "fooForm" }))
{
    <input type="text" id="username"/>
}

<script type="text/javascript">
    $(document).ready(function () {
        $(".jqselect").click(function () {

            $.ajax({
                url: '/Account/LogOn',
                type: "Post",
                data: $('#fooForm').serialize(),
                success: function (result) {
                    if (result.success) {
                    }
                }
            });

        });
    });
</script>

[HttpPost]
public ActionResult LogOn(string username)
{
    Console.WriteLine(username);
    return new EmptyResult();
}

1 Answer 1

3

You have to give it a name:

<input type="text" id="username" name="username"/>

You can actually drop the id and just have the name and the controller method will recognize it.

You can also use a model if you want and you can code it like:

@Html.TextboxFor(m=>m.Username)

where your model is defined as:

public class LogOnModel {
    public string Username {get;set;}
}

and your method defined as:

[HttpPost]
public ActionResult LogOn(LogOnModel input)
{
}
Sign up to request clarification or add additional context in comments.

Comments

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.