Base solution for your next web application
Open Closed

How to implement a standard @Html.DropDownList #3853


User avatar
0
Garysund created

Hi There

This may be a very noob question but I am struggling to implement a standard @Html.DropDownListFor. Does anybody have ant examples or can show me how to implement this.

Modal Code

<label for="ParentId">@L("Parent Category")</label>
            @Html.DropDownList("ParentId", Model.CategoryList.Select(i => i.ToSelectListItem()), new { @class = "form-control edited" })

Controller

public async Task<PartialViewResult> CreateOrEditModal(int? id)
        {
            var output = await _categoryAppService.GetCategoryForEdit(new NullableIdDto { Id = id });
            var viewModel = new CreateOrEditCategoryModalViewModel(output);

            return PartialView("_CreateOrEditModal", viewModel);
        }

APP Service

public async Task<GetCategoryForEditOutput> GetCategoryForEdit(NullableIdDto input)
    {
        var categorylist = _categoryRepository.GetAll().ToList();
        CategoryEditDto categoryEditDto;

        if (input.Id.HasValue) //Editing existing role?
        {
            var category = await _categoryRepository.GetAsync(input.Id.Value);
            categoryEditDto = category.MapTo<CategoryEditDto>();
        }
        else
        {
            categoryEditDto = new CategoryEditDto();
        }

        return new GetCategoryForEditOutput
        {
            Category = categoryEditDto,
            CategoryList = categorylist.
           
        };
    }

DTO

public class GetCategoryForEditOutput
    {
        public CategoryEditDto Category { get; set; }

        public List<ComboboxItemDto> CategoryList { get; set; }

       
    }

1 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    hi,

    you didn't mention what the exception is ... but as i see you miss the extension of partial view (cshtml)

    Right code:

    public async Task<PartialViewResult> CreateOrEditModal(int? id)
            {
                var output = await _categoryAppService.GetCategoryForEdit(new NullableIdDto { Id = id });
                var viewModel = new CreateOrEditCategoryModalViewModel(output);
    
                return PartialView("_CreateOrEditModal.cshtml", viewModel);
            }