我有Users模型类:
public partial class Users
{
public Users()
{
Seasons = new HashSet<Seasons>();
UserRoles = new HashSet<UserRoles>();
UserSetting = new HashSet<UserSetting>();
}
public int? Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
public DateTime? EmailVerifiedAt { get; set; }
public DateTime? MobileVerifiedAt { get; set; }
public DateTime CreatedAt { get; set; }
public virtual ICollection<Seasons> Seasons { get; set; }
public virtual ICollection<UserRoles> UserRoles { get; set; }
public virtual ICollection<UserSetting> UserSetting { get; set; }
}
另一个类是:
public partial class UserRoles
{
public int? UserId { get; set; }
public int? RoleId { get; set; }
public int Id { get; set; }
public virtual Roles Role { get; set; }
public virtual Users User { get; set; }
}
我想使用发帖请求将数据发送到控制器,我有一个用户,因此我从发帖人那里发送数据
{
"FirstName":"Jignesh",
"LastName":"Jignesh",
"Email":"samjad@gmail.com",
"Mobile":"99999999",
"Password":"sss",
"UserRoles[0]":1
}
在控制器上,我编写了这样的代码。但是UserRoles是一个空数组
[HttpPost]
public ActionResult PostUsers(Users users)
{
foreach(var role in users.UserRoles)
{
var sql = "insert into
UserRoles(UserId,RoleId)values('{users.Id}','{role}')";
Connection.ExecuteCommand(sql);
}
}
如何在控制器中接收多个RoleId
答案 0 :(得分:0)
我想这就是你想要的:
{
"FirstName":"Jignesh",
"LastName":"Jignesh",
"Email":"samjad@gmail.com",
"Mobile":"99999999",
"Password":"sss",
"UserRoles": [
{ "Id": 1 },
{ "Id": 2 }
]
}