locked
populate dropdown on view from another model RRS feed

  • Question

  • User1879907140 posted

    I have a view that I need to display a drop down. The data will come from a different model that what the view is using. How can I display that data in my drop down from that model? 

    view:

    @model Dealers.Models.NewMakes
    @model Dealers.Models.Dealers
    
    <select asp-for="Dealers.NewDealers"/>
    
    

    I need to show the dealers in my dropdown from the Dealerd model. I get NewMakes does not contain a definition of NewDealers. Do  I need to make a reference to it in the NewDealers controller?

    I'm using EF, .NET CORE MVC

    Monday, June 29, 2020 2:50 PM

All replies

  • User-474980206 posted

    a view can only have one model.

    you should add the dropdown data to the view model or pass via the viewbag. say change the model to:

    public MyViewModel
    {
        public Dealers.Models.NewMakes NewMakes {get; set;}
        public Dealers.Models.Dealers Dealers {get; set;}
    }
    

    and construct in the controller.

    Monday, June 29, 2020 3:06 PM
  • User-1330468790 posted

    Hi GeorgeTheCoder,

     

    I am not sure why you use two @model in the view but definitely it won't be working.

    The best way is as @bruce said - using a view model for the view.

    If you want to populate new dealers to the drop down list, the only thing you need to do is changing the value of the model.

    More details about model binding of the select tag, you could refer to below codes.

    Index.cshtml

    @model YourProject.Models.DealersViewModel;
    @{
        ViewData["Title"] = "Home Page";
    }
    
    <form method="post">
        <div class="text-center">
            <h1 class="display-4">Change to new Dealers</h1>
            <select asp-for="DealerType" asp-items="Model.DealerTypes" ></select>
    
            <button asp-controller="Home" asp-action="Index">Get Dealer List</button>
    
            <select asp-for="Dealer" asp-items="Model.Dealers" ></select>
        </div>
    </form>

    HomeController:

    [HttpGet]
            public IActionResult Index()
            {
                var model = new DealersViewModel();
                model.DealerType = "Old";
                model.Dealers = new List<SelectListItem>() { 
                     new SelectListItem { Text = "Old Dealer1", Value = "Old Dealer1" },
                     new SelectListItem { Text = "Old Dealer2", Value = "Old Dealer2" },
                     new SelectListItem { Text = "Old Dealer3", Value = "Old Dealer3" }
                };
                model.DealerTypes = new List<SelectListItem>() {
                     new SelectListItem { Text = "Old", Value = "Old" },
                     new SelectListItem { Text = "New", Value = "New" }
                };
                return View(model);
            }
    
            [HttpPost]
            public IActionResult Index(DealersViewModel model)
            {
                if (ModelState.IsValid)
                {
                    if (model.DealerType.Equals("New"))
                    {
                        model.Dealers = new List<SelectListItem>() {
                            new SelectListItem { Text = "New Dealer1", Value = "New Dealer1" },
                            new SelectListItem { Text = "New Dealer2", Value = "New Dealer2" },
                            new SelectListItem { Text = "New Dealer3", Value = "New Dealer3" }
                        };
    
                    } else
                    {
                        model.Dealers = new List<SelectListItem>() {
                            new SelectListItem { Text = "Old Dealer1", Value = "Old Dealer1" },
                            new SelectListItem { Text = "Old Dealer2", Value = "Old Dealer2" },
                            new SelectListItem { Text = "Old Dealer3", Value = "Old Dealer3" }
                        };
                    }
    
                    model.DealerTypes = new List<SelectListItem>() {
                        new SelectListItem { Text = "Old", Value = "Old" },
                        new SelectListItem { Text = "New", Value = "New" }
                    };
    
                }
                   
                
                return View(model);
            }

    Model:

     public class DealersViewModel
        {
            public string Dealer { get; set; }
            public string DealerType { get; set; }
            public List<SelectListItem> DealerTypes { get; set; }
            public List<SelectListItem> Dealers { get; set; }
        }
    

    Result:

    If you still have concerns about the problem, please post the related codes so that we could help you solve the problem in that certain scenario. 

     

    Hope this can help you.

    Best regards,

    Sean

    Wednesday, July 1, 2020 9:34 AM