Solution 1 :

I’m making a lot of assumptions, however if you are using a dynamic object type (ExpandoObject()) in your controller to return multiple models, then you should be able to programmatically aggregate the desired data (Criteria) on the view side using Razor into one list and reference that list with the asp-items tag.

This is what I’m meaning, something like this:

    <select class="form-control col-4 d-inline" asp-for="Criteria" id="savedCriteria" asp-items="@(new SelectList(@Model1.Criteria.Union(@Model2.Criteria).Distinct()))">
        <option>Testing text 1</option>
    </select>

I haven’t tested this code, but it’s the idea I was going for.

Solution 2 :

I don’t know if there’s an approach for binding multiple models into PartialViews.

But I do recommend utilizing ViewModels instead. Since your 2 models above use the same property (Criteria), just create a ViewModel with property common to those models you have and passed this ViewModel to your View (use ViewBag for passing the ViewModel to your View, if preferred).

From there, passed it to your PartialView.

Solution 3 :

Try to receive List<string> as model for your partial view, and then use below code:

@model List<string>
<h1>_testPartial</h1>

<div class="row">
    <div class="col-10 propertiesdiv">
        <label><b>Saved Criteria: </b></label><br />
        <select class="form-control col-4 d-inline" name="Criteria" id="savedCriteria" asp-for="@Model">
            <option>Testing text 1</option>              
        </select>
    </div>
</div>
<br />

For each main View, pass Model.Criteria to partial view.

@model Model1

<partial name="_testPartial" model="Model.Criteria"/>

Problem :

How can I create a select dropdown list that can bind to multiple different models that have a property with a matching name? In the example below I used Criteria as the matching property that is included in each model.

Partial View

<div class="row">
    <div class="col-10 propertiesdiv">
        <label><b>Saved Criteria: </b></label><br />
        <select class="form-control col-4 d-inline" asp-for="Criteria" id="savedCriteria"><option>Testing text 1</option></select>
    </div>
</div><br />

There is an error coming from the text asp-for="Criteria" that states “Represents an object whose operations will be resolved at runtime. An expression tree may not contain a dynamic operation.”

Model 1 Example

public class Model1
{
    public IEnumerable<string> Criteria { get; set; }
}

Model 2 Example

public class Model2
{
    public IEnumerable<string> Criteria { get; set; }
}

Comments

Comment posted by davepaquette.com/archive/2015/05/18/mvc6-select-tag-helper.aspx

davepaquette.com/archive/2015/05/18/mvc6-select-tag-helper.aspx

Comment posted by Robert Harvey

Could inheritance help here?

Comment posted by sjohn285

I’ve got over 20 models that will all have this Criteria list. I used model1 and model2 as some simple examples to illustrate the general process that I was wanting. I’m not sure this way of binding the models will be effective with such a large number of models.

Comment posted by EspressoBeans

Oh man, with 20 models, yeah the above is not scalable. What is keeping you from combining those into one list on the controller side rather than the view server page?

Comment posted by sjohn285

@EspressoBeans the list is unique to each model. I might not be understanding your question correctly, but this list of objects is generated differently depending on the model that is being displayed in the view. Are you thinking if I make a new controller method that takes a string parameter with the model name, I could return the list that I’m looking for?

Comment posted by EspressoBeans

Ah ok, let me ask, all these models that have criteria list, are they passed to the view all at once (combined “god object”), or are they displayed singularly depending on what is desired to be viewed on the web page?

Comment posted by sjohn285

They are displayed singularly depending on the model.

By