I am creating a system where when a user is created a device can be assigned to that user. I am doing this with a dropdown and while the dropdown is populated with the list of devices, it does not update the user with that device. Multiple users can use the same device and i want to avoid hard coding as the information is already on the database.

I am doing this through ef code first any other suggestions will be taken into account. Im really just focused on getting this to work so i am open to suggestions. I also want to get this working with edit, however im assuming the solution will fix both create and edit, if i use similar code.

I have been using a viewmodel to display the devices on the index page. The current users on the db show their allocated devices so i know that works, but i had to manually assign the devices in the table data.

**PatientController.cs (users are also known as patients)**

```
namespace FaceToFaceWebsite.Controllers
{
    public class PatientController : Controller
    {
        private F2FData db = new F2FData();

        public ActionResult Index()
        {
            var viewModel = db.Users
                .Select(u => new UserDeviceViewModel() 
            { 
                User = u,
                Device = u.Device

             }).ToList();

            return View(viewModel);
        }

        public ActionResult Create()
        {
            ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name");

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "UserID,CodeName,UseBriefInstructions,DeviceID,Name")] User user, Device device)
        {
            if (ModelState.IsValid)
            {

                db.Users.Add(user);
                db.Devices.Attach(device);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(user);
        }

```

**User.cs (in another project, but in the same solution)**

```
namespace FaceToFace.Model
{
    public class User
    {
        public int UserID { get; set; }
        public string CodeName { get; set; }
        public bool UseBriefInstructions { get; set; }
        public ICollection<RegimeItem> RegimeItems { get; set; }
        public Device Device { get; set; }
        public virtual ICollection<Grading> UserGradings { get; set; }
        public User()
        {
            this.RegimeItems = new List<RegimeItem>();
            Device = new Device();
        }   
    }
    public class RegimeItem
    {
        public int RegimeItemID { get; set; }
        public Exercise RegimeExercise { get; set; }
    }
}

```

**Device.cs (in another project, but in the same solution)**

```
namespace FaceToFace.Model
{
    public class Device
    {
        public int DeviceID { get; set; }
        public string Name { get; set; }
    }
}

```

**UserDeviceViewModel.cs**

```
namespace FaceToFaceWebsite.Models
{
    public class UserDeviceViewModel
    {
        public Device Device { get; set; }
        public User User { get; set; }
        public IList<SelectListItem> Name { get; set; }
    }
}

```

**View/Patient/Create.cshtml**

```
    @model FaceToFace.Model.User

    @{
        ViewBag.Title = "Create";
    }

    <h2>Create</h2>

    @using (Html.BeginForm()) {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
            <legend>User</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.CodeName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.CodeName)
                @Html.ValidationMessageFor(model => model.CodeName)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Device.Name, "Device")
            </div>
            <div class="editor-field">
            @Html.DropDownListFor(model => model.Device.DeviceID, (IEnumerable<SelectListItem>)ViewBag.DeviceID)

            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.UseBriefInstructions)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.UseBriefInstructions)
                @Html.ValidationMessageFor(model => model.UseBriefInstructions)
            </div>

            <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

```

I believe the problem lies with what ive been doing in the Controller, and after many tries i cannot get the create function to assign a user to a device from the db so any help would be greatly appreciated.

**UPDATE**

**PatientController.cs**

```
private void PopulateDeviceChoices(UserDeviceViewModel model)
{
    model.DeviceChoices = db.Devices.Select(m => new SelectListItem
    {
        Text = m.Name,
        Value = m.DeviceID
    });
}
        //
        // GET: /Patient/
        public ActionResult Index()
        {
            //var viewModel = new List<FaceToFaceWebsite.Models.UserDeviceViewModel>();
            var viewModel = db.Users
                .Select(u => new UserDeviceViewModel()
            {
                User = u,
                Device = u.Device

            }).ToList();

            return View(viewModel);
        }

        public ActionResult Create(UserDeviceViewModel model, User user)
        {
            ViewBag.DeviceID = new SelectList(db.Devices, "DeviceID", "Name");

            var device = db.Devices.Find(model.DeviceId);
            user.Device = device;

            PopulateDeviceChoices(model);
            return View(model);
        }

        //
        // POST: /Patient/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(User user, Device device, UserDeviceViewModel model)
        {
            if (ModelState.IsValid)
            {

                db.Users.Add(user);
                db.Devices.Attach(device);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            PopulateDeviceChoices(model);
            return View(model);
        }

```

I have made the changes as Chris' suggestions, however i dont think i have translated them correctly, as i am getting an error at `Value = m.DeviceID` which is telling me `Cannot implicitly convert type 'int' to 'string'`

I have got it working however, i am getting two sets of user input rows on the view, it also does not allow me to assign a device to a user still. ![image of create view below](https://i.stack.imgur.com/Lagqv.jpg)

**Update 2 (Stephens suggestions)**

**PatientController.cs**

```
public ActionResult Create()
        {
            UserDeviceCreateViewModel model = new UserDeviceCreateViewModel();
            ConfigureViewModel(model);
            return View(model);
        }

        public ActionResult Create(UserDeviceCreateViewModel model, User user, Device device)
        {
            if (!ModelState.IsValid)
            {
                ConfigureViewModel(model);
                return View(model);  
            }
            db.Users.Add(user);
            db.SaveChanges();
            return RedirectToAction("Index");

        }

```