DropDownlist in Modal popup

1k Views Asked by At

I am tryingto populate a DropDownlist with the viewbag defined in the controller and i used a modal popup :

ProductController.cs

public IActionResult Create()
    {
        List<Category> categories = _dbcontext.Category.ToList();
        ViewBag.bpCategories = new SelectList(categories, "CategoryId", "CategoryName");
        Product product = new Product();
        return PartialView("_AddProductPartialView",product);
    }

_AddProductPartialView.cshtml:

    @model WebApplication1.Models.Product

<div class="modal fade" role="dialog" tabindex="-1" id="addProduct" aria-labelledby="addProductLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addProductLabel">Products</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form asp-action="Create" method="post">
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    ......

                    <div class="form-group">
                        <label asp-for="CategoryId" class="control-label">CategoryId</label>

                        @Html.DropDownListFor(m => m.CategoryId, ViewBag.bpCategories as IEnumerable<SelectListItem>, "--Select ---", new { @class = "form-control" })

                    </div>
                    ...

                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">Save</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

But i got this error:

The ViewData item that has the key 'CategoryId' is of type 'System.Int32' but must be of type 'IEnumerable'.

So what is the correct way to populate a DropDownlist in a modal popup??

EDIT

I tried also this code but i got an empty Dropdownlist:

<select asp-for="CategoryId" class="form-control" asp-items="ViewBag.bpCategories"></select>
2

There are 2 best solutions below

3
Fei Han On

Following code snippet with testing data can populate dropdown well, you can refer to it.

List<Category> categories = new List<Category>
{
    new Category
    {
        CategoryId = 1,
        CategoryName = "Category1"
    },
    new Category
    {
        CategoryId = 2,
        CategoryName = "Category2"
    },
    new Category
    {
        CategoryId = 3,
        CategoryName = "Category3"
    }
};

ViewBag.bpCategories = categories.AsQueryable().Select(s => new SelectListItem { Value = s.CategoryId.ToString(), Text = s.CategoryName }).ToList();

In view page

<select asp-for="CategoryId" class="form-control" asp-items="ViewBag.bpCategories as List<SelectListItem>"></select>

Test Result

enter image description here

0
vinay On

In Controller :

        List<CollectionDetails> CollectionData = new List<CollectionDetails>();
        CollectionData = GetCollectionDetails();
        
        TempData["Department"] = new SelectList(CollectionData, "CollectionId", 
        "CollectionName");

In ViewModal :

                           @Html.DropDownListFor(model => model.CollectionId, new SelectList(TempData["Department"] as System.Collections.IEnumerable, "value", "text"), ---please select---, new { @tabindex = 13, @class = "form-control" })