I have these classes:
public class User
{
[Key]
public Guid Id { get; set; }
}
public class Computer
{
public int Id { get; set; }
public virtual ICollection<User> Users { get; set; }
}
My action looks something like this:
public ActionResult Create([Bind(Include = "Users")] Computer computer)
{
if (ModelState.IsValid)
{
...
}
var userList = _useRepository.GetAll();
ViewBag.travelers = new MultiSelectList(userList, "Id", "Name");
return View(travelRequest);
}
My view:
@Html.ListBox("Travelers", (MultiSelectList)ViewBag.travelersList, new { @class = "form-control" })
This question is very similar to this and the solution might still work. Is there a way to do this but avoid adding the items to the collection.
Additionally I have this
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
modelBuilder.Entity<TravelRequest>()
.HasMany(x => x.Travelers)
.WithMany()
.Map(m =>
{
m.MapLeftKey("TravelReqeustId");
m.MapRightKey("UserId");
m.ToTable("Travelers");
});
}
I have tried without the modification in OnModelCreating
but I still get the same error.
Validation fails and says value is invalid.
Exception {"The parameter conversion from type 'System.String' to type
'User' failed because no type converter can convert
between these types."} System.Exception {System.InvalidOperationException}