tags:

views:

93

answers:

3

Hello

I'm having problems with binding values from a view to an object. Somehow the object gets all values set to null then.

In my viewmodel I have:

public class UserAdminEditViewModel
{
    public User User { get; set; }
    public IEnumerable<Usergroup> Usergroups { get; set; }
    public IEnumerable<User> Users { get; set; }

    public UserAdminEditViewModel() {}
}

In my controller I have:

[AcceptVerbs(HttpVerbs.Post)]       
public ActionResult UserEdit(
    [BindAttribute(
        Include="UserID,UserName,Email,Password,Firstname,Surname")]
    UserAdminEditViewModel User, 
    int[] UsergroupID)
{        
    if (ModelState.IsValid)
    {
        try
        {
            for(int i = 0; i < UsergroupID.Length; i++)
            {                         
                User.User.Usergroups.Add(
                    _ug.GetUsergroups(UsergroupID[i]).First());    
            }

            _us.SaveUser(User.User);

And in my edit-view I have:

<%= Html.TextBox("User.Username", Model.User.Username) %>
<%= Html.TextBox("User.Firstname", Model.User.Firstname) %>

etc etc..

I have tried with:

<%= Html.TextBox(
        "User.Username", Model.User.Username, new { id = "User.Username" } ) %>

etc, but that doesn't work either... Only works when I remove BindAttribute...

How can I fix this, I can't pass all the models columns...

Thanks in advance /M

A: 

To use an attribute, you must specify its name without the "Attribute" part of the class name, like:

public ActionResult UserEdit([Bind(Include="UserID,UserName,Email,Password,Firstname,Surname")]UserAdminEditViewModel User, int[] UsergroupID)
{

For instance, AcceptVerbs is also an attribute, and the related class is called AcceptVerbsAttribute...

One more detail: the Controller class contains a property called User, which represents the authenticated user who initiated the call to the action method. If you create a parameter named User, you will simply hide the "default" User property. You might want to change the name of your parameter!

Otherwise, you can still access the controller's User property through

base::User
Bruno Reis
I changed to: [Bind(Include = "UserID,Username,Email,Password,Firstname,Surname")]UserAdminEditViewModel userobj same result :(
molgan
Using the Attribute suffix is optional. They are equivalent statements.
BC
A: 

Have you tried adding the prefix to the Bind attribute, either as the Prefix property or as part of the Includes, e.g:

public ActionResult UserEdit([Bind(Include="User.UserID,User.UserName,User.Email,User.Password,User.Firstname,User.Surname")]User User, int[] UsergroupID)
Kieron
Tried that aswell, same result, all are null
molgan
In your view, change: <%= Html.TextBox("User.Username", Model.User.Username) %> <%= Html.TextBox("User.Firstname", Model.User.Firstname) %>to: <%= Html.TextBox("User.Username") %> <%= Html.TextBox("User.Firstname") %>The binder isn't getting a chance to work, you're override it.
Kieron
Same result, objects come back as null
molgan
Have you inspected the forms collection? See what's being posted back? - should give you an idea about what incantation to use on the bind attribute.
Kieron
how would that parameter list look like?
molgan
A: 

Passed in the wrong class to the postback action

molgan