如何绑定对象的列表以将其传递给控制器?
how to bind object's list to pass it to controller?
我创建了一个显示用户列表的表单。单击列表中的项目时,必须将所属属性传递给控制器。为此,我添加了以下 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)
我检查了数据是否正确传递给查看,似乎没问题:
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]]"}
然后,当对象被传递到控制器中的以下 UserEdit 方法时,所有属性都已正确设置但所有权列表为空:
public ActionResult UserEdit(DemoRes.Models.User user)
{
//here user.Ownership is empty
}
这是用户 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; }
}
有谁知道正确绑定所有 class 属性(基元和复杂类型,例如列表)以便将它们从视图传递回控制器的正确方法是什么?
您不能通过单个查询字符串 Ownership=item.Ownership
post 整个模型。 Post 整个模型由 Form
付诸行动。
我创建了一个显示用户列表的表单。单击列表中的项目时,必须将所属属性传递给控制器。为此,我添加了以下 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)
我检查了数据是否正确传递给查看,似乎没问题:
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]]"}
然后,当对象被传递到控制器中的以下 UserEdit 方法时,所有属性都已正确设置但所有权列表为空:
public ActionResult UserEdit(DemoRes.Models.User user)
{
//here user.Ownership is empty
}
这是用户 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; }
}
有谁知道正确绑定所有 class 属性(基元和复杂类型,例如列表)以便将它们从视图传递回控制器的正确方法是什么?
您不能通过单个查询字符串 Ownership=item.Ownership
post 整个模型。 Post 整个模型由 Form
付诸行动。