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.
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:
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.
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
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.