Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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();
}
share|improve this question

1 Answer

up vote 3 down vote accepted

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)
{
}
share|improve this answer
Arf yes, I'm stupid :( Thanks – Kris-I Mar 29 at 8:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.