0

I have a view model, controller. The view model has a selectlistitem property. I am using the selectlistitem to bring the list in the controller and passing it to view model list property.

In the text of selectlistitem, I am concatenating three properties. I am passing the list to viewmodel. I need to iterate through the list in the view and split the text to display it into what I needed. Is there any other approach where I can loop through the selectlistitem property and split the text?

ViewModel:

Public List<SelectlistItem> Current {get; set};

Controller:

public ActionResult Index()
{
    ViewModel model;

    model = new ViewModel()
    { 
        Current = Active
    };
    return View(model);
}

//to get data, a private property 
private List<SelectListItem> Active
{
    get
    {
        List<SelectListItem> active = new List<SelectListItem>();
        foreach(DomainModel model in domainlist)
        {
            active.Add(new SelectListItem)
            {
                Text = model.property + "-" +  model.property + "-" + model.property
            }
        }
        return active;
    }
}

I get the data back to view model. The text property in selectlistitem has 3 values combined. I want them to be separate. Is there a way, I can loop through the list item in viewmodel or controller and split the text rather than doing it in view? Or should i create another viewmodel and bring it as a list?

Tim
  • 398
  • 2
  • 6
  • 13
  • Your problem you use the generic SelectListItem as ViewModel property - you need to pass the DomainModel as object. Probably you used the OdeToCode example (first and most wrong one ever) you can find; read further here https://stackoverflow.com/questions/3057873/how-to-write-a-simple-html-dropdownlistfor for more instructions and how to proceed (remember the ode-to-code exampe is almost 7 years old) – riffnl Sep 25 '19 at 22:23
  • I get it. I used the selectlistitem as the requirement earlier included a dropdownlist. I think I need to pass the domain model as object directly to view model and can bring that list displayed. – sandStorm44 Sep 26 '19 at 17:01

0 Answers0