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");
}
}
}