Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
How do I pass complex types to Html.LabelFor?

It doesn't work like this, the error is:

Expected ","

    @Html.LabelFor(ViewBag.OrganizationDetails => (string)ViewBag.OrganizationDetails.AddressLegal, htmlAttributes: new { @class = "control-label col-md-2" })

The OrganizationDetails is a class and the AddressLegal is a string (need its value)

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try this:

@model ParentModel

@Html.LabelFor(model => model.Name)
@Html.LabelFor(model => model.Child.Name)

in your case should be:

@model ViewBag.OrganizationDetails

@Html.LabelFor(model => model.AddressLegal, new { @class = "control-label col-md-2" })

or

@model ViewBag

@Html.LabelFor(model => model.OrganizationDetails.AddressLegal, new { @class = "control-label col-md-2" })

I haven't tried it.

over 4 years ago · Santiago Trujillo Relatório

0

You have two choices.

The first, when using the ViewBag:

@using Models
@{
    ViewBag.Title = "Home Page";
    OrganizationDetails details = ViewBag.OrganizationDetails;
}

@Html.LabelFor(m => details.AddressLegal, htmlAttributes: new { @class = "control-label col-md-2" })

The second, use the strongly typed version of the Html.LabelFor() by passing the model to the view:

public ActionResult Index()
{
    var model = new OrganizationDetails() { AddressLegal = "Address" /* set another properties ... */};    
    return View(model);
}

And in the view:

@model Models.OrganizationDetails

@Html.LabelFor(m => m.AddressLegal, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.DisplayFor(m => m.AddressLegal)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda