No argument was given that corresponds to the required formal parameter 'htmlAttributes' of 'IHtmlHelper - DropDownListFor

Viewed 406

I am having an issue which I haven't resolved at this point and I can't really find any answers. The error that I am getting, when I try to construct the dropdown list is this:

 No argument was given that corresponds to the required formal parameter 'htmlAttributes'
 of 'IHtmlHelper<ContactUsPageModel>.DropDownListFor<TResult>(Expression<Func<ContactUsPageModel, TResult>>, IEnumerable<SelectListItem>, string, object)'

The code in my razor View page ContactUs.cshtml has the next structure:

@Html.DropDownListFor(model => model.Provincia,  PoblacionesExtensions.ToSelectList(ViewBag.locations), new{@class="form-control form-control-login font-weight-bold", @style="padding:0"} )  

Inside the View Task, I am utilizing the GetCurrentLocation() function which gets all the provinces that I need in order to display to my dropdown list. I pass all the locations to a ViewBag var.

The code of my Controller is this:

    [AllowAnonymous]
    public async Task<IActionResult> ContactUs()
    {
        var data = new ContactUsPageModel();
        var user = await userSessionVars.GetCurrentUser();
        var locationsVar = await userSessionVars.GetCurrentLocation();
        ViewBag.locations = locationsVar;


        if (user != null)
        {
            data = new ContactUsPageModel()
            {
                Name = $"{user.Nombre} {user.Ape1} {user.Ape2}",
                Phone = user.UserName,
                Email = user.Email,
            };
        }

        return View(data);
    }  

This task comes from my Helper. It is connected to one of the services of the Entity and gets all the locations that I need. It makes the request, receives the JSON and then deseriazes it:

   public async Task<List<string>> GetCurrentLocation(){
        var ConsultaLocalidadesEsp = await ServiceEntityHub.ConsultaLocalidadesEsp();
        var locations =  JsonConvert.DeserializeObject<List<PoblacionDto>>(ConsultaLocalidadesEsp);
        return  locations.Select(x => x.provincia).ToList();
    }

At last, I am constructing and returning the SelectList through another Helper that I made. It is located inside the /Helper/ directory. I return it to the dropdown list html element, inside my View razor page which I have pointed out at the very top of the question.

using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic;
using System.Linq;

namespace Asistencia.Helper
{
    public static class PoblacionesExtensions
    {

        public static SelectList ToSelectList(this List<string> poblaciones )
        {
            if(!poblaciones.Contains("Selecciona tu provincia"))
            {
                poblaciones.Add("Selecciona tu provincia");
            }

            return new SelectList(poblaciones, "Selecciona tu provincia");

        }

    }
}
1 Answers

I finally found the solution to my problem. What I had to do finally was to use the IHtmlHelper DropDownList instead of DropDownListFor.

My razor View code:

@Html.DropDownList("Provincia",  PoblacionesExtensions.ToSelectList(ViewBag.locations), optionLabel: null, htmlAttributes: new{@class="form-control form-control-login font-weight-bold p-0"} )  

My Controller's code for returning the view with the model I applied to it.

   [AllowAnonymous]
    public async Task<IActionResult> ContactUs()
    {
        var data = new ContactUsPageModel();
        var user = await userSessionVars.GetCurrentUser();
        var locationsVar = await userSessionVars.GetCurrentLocation();
        ViewBag.locations = locationsVar;

        if (user != null)
        {
            data = new ContactUsPageModel()
            {
                Name = $"{user.Nombre} {user.Ape1} {user.Ape2}",
                Phone = user.UserName,
                Email = user.Email,
                Provincia = locationsVar
            };
        }

        return View(data);
    }

Here is the task from the service I am using to get the locations for my dropdown. I had to explicitly declare the final received locations variable as type of List<string>.

 public async Task<List<string>> GetCurrentLocation()
    {
        var ConsultaLocalidadesEsp = await ServiceEntityHub.ConsultaLocalidadesEsp();
        var locations =  JsonConvert.DeserializeObject<List<PoblacionDto>>(ConsultaLocalidadesEsp);
        List<string> locationsFinal = locations.Select(x => x.provincia).Distinct().ToList();
        return locationsFinal;
    }

As a final observation, the initial problem I think was with the DropDownListFor IHtmlHelper which was creating the issue and thus by applying this new approach it solved my problem.

Related