Search code examples
c#asp.net-mvclinqasp.net-mvc-partialviewasp.net-mvc-viewmodel

MVC - Populate Two Dropdowns From Database in Partial View with ViewModel


Here's the visual of what I'm trying to do.

enter image description here

So basically what happens here is that you choose a name from the select on the left and a team from the select on the right, then click Submit Assignment and it adds a record. (This information is for reference so you can understand the context. I don't need help with the database inserts or updates.)

This is going to be a PartialView with a ViewModel.

I need to know how to populate those two dropdowns with separate lists. I'm assuming those would be coming over in the ViewModel.

So in my main view, I have a call to the PartialView with RenderAction.

                    //The action       //The controller
@{Html.RenderAction("TeamAssignment", "TeamAssignment");}

The controller that gets called

public class TeamAssignmentController : Controller
    {
        MyDatabaseEntities db = new MyDatabaseEntities();

        [ChildActionOnly]
        public ActionResult TeamAssignment()
        {
            //NEED A VIEW MODEL HERE TO PASS INTO THE PARTIALVIEW
            return PartialView();
        }
}

In the ViewModel I will need lists that populate the two drop downs.

The linq query on the left for Name would be this.

var nameList = (from p in db.Participant
                where p.ParticipantType == "I"
                select new {
                    p.ParticipantID,
                    p.IndividualName
                }).ToList();

The linq query on the right for Team would be this.

var teamList = (from p in db.Participant
                where p.ParticipantType == "T"
                select new {
                    p.ParticipantID,
                    p.TeamName
                }).ToList();

How can I pass that information into the PartialView and how do I populate the dropdowns with the ID for value and name for display text?

UPDATE

I belive this may be the ViewModel I need. Is this correct?

public class TeamAssignmentViewModel
{
    //Need these ints for the updating and deleting
    public int IndividualParticipantID { get; set; }
    public int TeamParticipantID { get; set; }
    public List<Participant> NameList { get; set; }
    public List<Participant> TeamList { get; set; }
}

Solution

  • You need to create an instance of a viewmodel, populate it and pass it to your PartialView:

        [ChildActionOnly]
        public ActionResult TeamAssignment()
        {
            // Instantiate A VIEW MODEL HERE TO PASS INTO THE PARTIALVIEW
            MyCustomModel model = new MyCustomModel();
            model.nameList = (from p in db.Participant
                where p.ParticipantType == "I"
                select p).ToList();
            model.teamList = (from p in db.Participant
                where p.ParticipantType == "T"
                select p).ToList();
    
            return PartialView("TeamAssignment", model);
        }
    

    EDITED: removed use of dynamic class in linq statements

    The markup for the Dropdowns might look something like:

    @Html.DropDownListFor(model => model.IndividualParticipantID,
    new SelectList(Model.NameList, "ParticipantID", "FirstName")
    
    @Html.DropDownListFor(model => model.IndividualParticipantID,
    new SelectList(Model.TeamList, "ParticipantID", "Team")