[RESOLVED]DropDownlistFor using View model. MVC 5.1

I need a DropDownList  using view model to show list of workers. I made a ViewModel for that

   public class WorkersViewModel
    {
        public string WorkerName { get; set; }
       public int WorkerId { get; set; }
      public  List<SelectListItem> WorkersList{get; set;}

        public WorkersViewModel()
        {
            WorkersList = new List<SelectListItem>();
        }
    }

I need to use this dropdownlist in a strongly typed view which uses another class WorkerTask

 public partial class WorkerTask
    {
        public int WorkerID { get; set; }
        public int ProjectID { get; set; }
        public string Task { get; set; }
    }

 Parent View

@using Actreve.Domain;
@model Actreve.Domain.WorkerTask
@{
    ViewBag.Title = "AddWorkerTask";
}

<h2>AddWorkerTask</h2>

@using (Html.BeginForm("AddWorkerTask", "WorkerTask", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.HiddenFor(x=> x.ProjectID)
    @Html.HiddenFor( x=> x.WorkerID)

    <div class="form-horizontal"><div class="form-group"><label class="control-label col-xs-2">Worker: </label>Drop down list here</div></div>
    <div class="form-horizontal"><div class="form-group"><label class="control-label col-xs-2">Task:</label>@Html.TextBoxFor(x => x.Task, new { @class = "form-control col-xs-8" })</div></div>
    }

Controller

    public ActionResult AddWorkerTask()
        {
            var workerList = new WorkersViewModel();
            IEnumerable<Worker> workers = repository_worker.GetWorkers(1);
            workerList.WorkersList = workers.Select(x => new SelectListItem { Text = x.FirstName + " " + x.LastName, Value = x.WorkerID.ToString() }).ToList();
            return View();
            
        }

I was trying to make a partialview for the dropdown but I unable to pass the model from controller to the partial view.

I’m new to MVC and dropdownlist is killing me! Thanks for your help.

Rose

Hi , 

You need to pass the ViewModel to the View ,

public ActionResult AddWorkerTask()
        {
            var workerList = new WorkersViewModel();
            IEnumerable<Worker> workers = repository_worker.GetWorkers(1);
            workerList.WorkersList = workers.Select(x => new SelectListItem { Text = x.FirstName + " " + x.LastName, Value = x.WorkerID.ToString() }).ToList();
            return View(workerList);
            
        }
View:
  <div class="form-horizontal"><div class="form-group"><label class="control-label col-xs-2">Worker: </label>@Html.Action("WorkerListPartial", "WorkerTask")</div></div>

Controller:
       public ActionResult WorkerListPartial()
        {
            var workerList = new WorkersViewModel();
            IEnumerable<Worker> workers = repository_worker.GetWorkers(1);
            workerList.WorkersList = workers.Select(x => new SelectListItem { Text = x.FirstName + " " + x.LastName, Value = x.WorkerID.ToString() }).ToList();
            return PartialView(workerList);
        }

ok so I  made a separate action method for the partial view and called it through the parent body using child actions.

Leave a Reply