0

I need to populate a dropdown with some data i get from a SOAP server. The server provides me an array of the companies.

How would i use it to populate the DD ?

Here is my User class:

public class Usuario
{
    public string Nome { get; set; }
    public string Token { get; set; }
    public IEnumerable<SelectListItem> Unidades { get; set; }
}

Here is where i receive the companies and send it to the view, i get it from another Action that is redirecting to this Action:

var usuario = TempData["objUsuario"] as UsuarioSSO;

if (usuario == null) return RedirectToAction("Index", "Login");

if (usuario.UsuarioUnidades == null)
{
    ModelState.AddModelError("", "Usuário não possui unidades");

    return View();
}

var model = new Models.Usuario
{
    Unidades = usuario.UsuarioUnidades.ToList().Select(x => new SelectListItem
    {
        Value = x.CodigoEmitente.ToString(),
        Text = x.NomeFantasia
    })
};

return View(model);

Here is how i'm trying to display it:

@Html.DropDownListFor(x => x.Unidades, new SelectList(Model.Unidades))

I have already tried of everything but it won't work, i get some conversion errors and when i can make it work it won't display the content, it will only display the object inside the Text area

System.Web.Mvc.SelectListItem
1
  • 2
    I can't really undestand what "won't work" from your description, but the main problem I see if that you have not ogt a property on your model for the selected value. Once you have then the syntax is @Html.DropDownListFor(x => x.SelectedUnidadeId, new SelectList(Model.Unidades)) Commented Dec 11, 2014 at 11:49

2 Answers 2

4

You need to have one property for the selected item and the list of available items, e.g.:

public class Usuario
{
    public string Nome { get; set; }
    public string Token { get; set; }
    public string Unidade { get; set; }
    public IEnumerable<SelectListItem> Unidades { get; set; }
}

and then create the drop-down like:

@Html.DropDownListFor(x => x.Unidade, Model.Unidades)

You can directly supply the Unidades as it is already IEnumerable<SelectListItem>.

P.S.: I guessed the singular of Unidades as I do not speak your langauge, whatever it is. I recommend to ALWAYS use english in source code.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks buddy, worked like a charm ! Sorry about the language thing and by the way u guessed right. Just one question, this DD is inside a form that will be posted to my Action but i'm getting a invalid modelstate
What error do you get exactly (update your question)?
4

Your model needs a value type property to bind the selected option to. If CodigoEmitenteis typeof int then you model property needs to be

public int SelectedUnidades { get; set; }

and you need to assign the SelectList to another property in your view model or to a ViewBag property

ViewBag.UnidadesList = new SelectList(usuario.UsuarioUnidades, "CodigoEmitente", "NomeFantasia");

Then in the view

@Html.DropDownListFor(x => x.SelectedUnidades, (SelectList)ViewBag.UnidadesList)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.