I would create an object that would help in presenting what you are trying to output. It would be in the "model", but wouldn't be mapped to the database in any way. It would look something like this:
public class VehicleHelper //or whatever you would like to call it...
{
public Vehicle Vehicle { get; set; }
public bool Ticked { get; set; }
}
Then your Owner
would have a list of these VehicleHelpers
like so:
public class Owner
{
//include the properties you listed here...
[NotMapped]
public IEnumerable<VehicleHelper> VehicleHelpers { get; set; }
}
Then the Controller would populate the VehicleHelpers
property as needed:
public ActionResult Edit(int id)
{
MyContext db = new MyContext();
Owner owner = db.Owners.FirstOrDefault(o => o.OwnerID == id);
if (owner == null) thrown new Exception("Invalid Owner ID");
owner.VehicleHelpers = db.Vehicles
.Select(v => new VehicleHelper() { Vehicle = v, Ticket = false });
foreach (Vehicle ownedVehicle in owner.OwnerVehicles.Select(ov => ov.Vehicle))
{
VehicleHelper helper = owner.VehicleHelpers
.FirstOrDefault(vh => vh.Vehicle == ownedVehicle);
if (helper == null) continue;
helper.Ticked = true;
}
return View(owner);
}
[Note: This following part is where I'm not certain the exact syntax, conventions and such, because I'm just learning MVC and Razer myself, but it should be close. Feel free to edit or suggest edits. ;)]
Then you would have an editor template for the VehicleHelper
, which would look something like this:
@model VehicleHelper
<div>
@Html.CheckBoxFor(vh => vh.Ticked)
@model.Vehicle.Name
@Html.HiddenFor(vh => vh.Vehicle.VehicleID)
</div>
I hope that at least helps point you in the right direction. ;)