Object Model Properties Not Assigned After Submitting The Form

I have view with UserSample model with two properties (Username, Password), All I want to achieve is to take these values from the textboxes and to store them into the object model and then in the Action method to take this object and manipulate it.

But here in this code the object properties in the action method Index are always null after submitting the form with filled text box fields.


public ActionResult Index(UserSample user) //this is the object which properties I want to be asigned.
            if (user != null)


            return View();


@using (@Html.BeginForm("Index","Login"))
            <td>Username: </td>
            <td>Password: </td>
            <td><input id="btnSubmit" type="submit" value="Submit" /></td>


Try adding this line to your view:

@model UserSample

You probable need to include the namespace there. MyApp.Models.UserSample for example.

Then you can use stronly typed html-helpers:

<td>@Html.TextBoxFor(model => model.UserName)</td>


<td>@Html.TextBoxFor(model => model.Password)</td>

You should decorate the Index(UserSample user) method with the HttpPost attribute:

public ActionResult Index()
    // Get method.
    UserSample model = new UserSample();
    return View(model);

public ActionResult Index(UserSample user)
    //  POST method.

When you post the form now, the UserSample object should be populated with the values from the form.

If you don't understand model binding, I suggest you look into it. This tutorial might get you started.

Need Your Help

Error java.lang.NoClassDefFoundError on org.springframework.webflow.util.RandomGuid

java linux tomcat classpath centos5

I am sorry, my question is stupid, but I am not able to answer it, as a java illiterate. I run a tomcat (5) on CentOS5 (for a CAS server), and when I try to open this URL

Injecting dependencies in a prototype bean

java spring dependency-injection

I am using lookup method injection in Spring 3 to inject a prototype bean into a singleton bean as described in the Spring docs here. Inside my prototype bean I have an @Autowired dependency on ano...