Niko Gamulin
Niko Gamulin

Reputation: 66565

how to bind object's list to pass it to controller?

I have created a form which displays the list of users. When an item on the list is clicked, the belonging properties have to be passed to controller. In order to do so, I have added the following ActionLink:

@Html.ActionLink(@item.Username.ToString(), "UserEdit", "Admin", new DemoRes.Models.User{ UserId = item.UserId, Email= item.Email, Username=item.Username, Password=item.Password, IsActive=item.IsActive, Ownership=item.Ownership}, null)

I have checked whether the data is passed corrctly to view and it seems OK:

item.Ownership
Count = 1

    [0]: 18878
item.Ownership.GetType()
{Name = "List`1" FullName = "System.Collections.Generic.List`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}

Then, when the object is passed to the following UserEdit method in controller, all the properties are set correctly but Ownership list is empty:

public ActionResult UserEdit(DemoRes.Models.User user)
    {
        //here user.Ownership is empty
    }

and this is User class:

public class User
{
    public User()
    {

    }

    [BsonId(IdGenerator = typeof(CombGuidGenerator))]
    public Guid UserId { get; set; }

    [BsonElement("Username")]
    public string Username { get; set; }

    [BsonElement("Password")]
    public string Password { get; set; }

    [BsonElement("Role")]
    public int Role { get; set; }

    [BsonElement("Email")]
    public string Email { get; set; }
    [BsonElement("FirstName")]
    public string FirstName { get; set; }
    [BsonElement("LastName")]
    public string LastName { get; set; }
    [BsonElement("AuthLevel")]
    public int AuthLevel { get; set; }
    [BsonElement("RememberMe")]
    public bool RememberMe { get; set; }
    [BsonElement("IsActive")]
    public bool IsActive { get; set; }
    [BsonElement("Note")]
    public string Note { get; set; }
    [BsonElement("Ownership")]
    public List<long> Ownership { get; set; }
}

Does anyone know what is the correct way to bind all class properties (primitives and complex types such as lists) correctly in order to pass them from view back to controller?

Upvotes: 0

Views: 83

Answers (1)

Mohsen Esmailpour
Mohsen Esmailpour

Reputation: 11544

You cannot post whole model via single query string Ownership=item.Ownership. Post whole model by Form to action.

Upvotes: 1

Related Questions