Vili
Vili

Reputation: 908

How to fill dropdowns in MVC5 editor templates?

I have a view with 3 partial views and viewmodel that I bring in with the main view

ViewModel:

public class FactoryViewModel{

    public FactoryViewModel()
    {
        FactoryA = new FactoryA();
        FactoryB = new FactoryB();
        FactoryC = new FactoryC();

    }
    public FactoryA FactoryA { get; set; }
    public FactoryB FactoryB { get; set; }
    public FactoryC FactoryC { get; set; }
}

View.cshtml:

@model Problem.Models.FactoryViewModel

@using (Html.BeginForm()) 
{
       @Html.EditorFor(@Model.FactoryA);
        @Html.EditorFor(@Model.FactoryB);
        @Html.EditorFor(@Model.FactoryC);

}

Factory model:

    public abstract class Factory
    {
        public Factory()
        {
            Projects = new List<Project>();
            Manager = new Person();
        }
        public string Description { get; set; }
        public Person Manager { get; set; }
        IList<Project> Projects { get; set; }

    }

I also have FactoryController that haves ActionMethod for returning the view and JsonResult to fetch all Persons from database.

My editor templates create dropdown selection for persons. My problem is: how to fill the dropdowns in the editorTemplates?

I could just create empty dropdowns and fill them from javascript in the main view? If I would like to fill the dropdowns using viewmodel, how can I give the values to @Html.EditorFor()?

Upvotes: 0

Views: 568

Answers (1)

Vili
Vili

Reputation: 908

I had the wrong idea about the whole thing. Editor templates were the solution

@using (Html.BeginForm()) 
{
   @Html.EditorFor(m => m.FactoryA)
   @Html.EditorFor(m => m.FactoryB)
   @Html.EditorFor(m => m.FactoryC)

}

from here Post a form with multiple partial views

Upvotes: 1

Related Questions