View with 2 models within a ViewModel

1

Objective : Manipulate 2 models in the view using a ViewModel.

I made a ViewModel to encapsulate the 2 models, but I can not use one of them.

ViewModel:

public class BoletoConfigViewModel
{
    public Boleto Boletos { get; set; }
    public ConfigCliente Config { get; set; }
}

Controller:

    var config = db.Config
        .SingleOrDefault(x => x.ClienteId == 3); //consulta com o DataBase

// I'm not sure this would be the best way to do this query.

    var viewModel = new BoletoConfigViewModel
    {
        Boletos = new Boleto(),
        Config = config
    };

    return View(viewModel);

Na view:

@model WMB.MVC.Extranet.ViewModel.BoletoConfigViewModel

@using (Html.BeginForm()) 
{
   @Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Boletos.DataVencimento, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Boletos.DataVencimento, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Boletos.DataVencimento, "", new { @class = "text-danger" })
            </div>
        </div>

..//bastante codigo acima criado pelo Scanffolding...
Agora onde da problema
<div>Configuração.:</div> @Html.DisplayFor(model => model.Config.cur_Configuracao.ToString());

I was asked to do with a partial view, etc ... But for learning purposes and wondering what else I'll need this is not possible?

Error:

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions
Riga 85:     <div>Configuração.:</div> @Html.DisplayFor(model => model.Config.cur_Configuracao.ToString());
    
asked by anonymous 25.01.2016 / 21:33

1 answer

3

The message is quite clear:

  

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

That is, you can only use @Html.DisplayFor with:

  • Common class variables;
  • Properties;
  • Arrays with one dimension;
  • Parameters with an index of a dimension.

In this case, you used .ToString() , which is not any of them: it's a method.

There are some options you can use:

1. The property without .ToString() :

@Html.DisplayFor(model => model.Config.cur_Configuracao)

2. Remove% with%:

@Model.Config.cur_Configuracao.ToString()
    
25.01.2016 / 22:33