0

I have read similar questions here about the error but I haven't found a solution, The error occurs when I define a Display Template for the String Class, from the Controller I return an Object of the class "Persona" that contains an int attribute.

Controller:

namespace Display_y_DisplayTemplates.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.cadena = "Cadena de caracteres";

            char[] nombres = { 'A','n','d'};

            ViewBag.persona = new Persona() {
                Nombre = new String(nombres),
                Edad = 27,
                Empleado = true,
                Nacimiento = new DateTime(1990, 12, 15)
            };
            return View();
        }
    }

    public class Persona {
        public String Nombre { get; set; }
        public int Edad { get; set; }
        public bool Empleado { get; set; }
        public DateTime? Nacimiento { get; set; }
    }
}

Index View: Index.cshtml

@{
    ViewBag.Title = "Home Page";
}

@*A Display se le pasa como parametro el nombre del ViewBag*@
<h1 style="color:red">Display con cadena de caracteres</h1>
@Html.Display("cadena")

@*A Display se le pasa como parametro el nombre del ViewBag*@
<h1 style="color:red">Display con objeto persona</h1>
@Html.Display("persona")

The Display Template for String class String.cshtml

@model String

<b>@Model</b>

And finally this is the Display Template for the "Persona" class Persona.cshtml

@model Display_y_DisplayTemplates.Controllers.Persona

<div class="form-group">
    @Html.LabelFor(Model => Model.Nombre):
    @Html.DisplayFor(Model => Model.Nombre)
</div>

<div class="form-group">
    @Html.LabelFor(Model => Model.Edad):
    @Html.DisplayFor(Model => Model.Edad)
</div>

<div class="form-group">
    @Html.LabelFor(Model => Model.Empleado):
    @Html.DisplayFor(Model => Model.Empleado)
</div>

<div class="form-group">
    @Html.LabelFor(Model => Model.Nacimiento):
    @Html.DisplayFor(Model => Model.Nacimiento)
</div>

I appreciate your collaboration

Andres Camacho
  • 367
  • 4
  • 17
  • The code you have shown is not resulting in that error. but this a dreadful way to pass data to the view. Pass your model (not using `ViewBag`) –  Jan 27 '18 at 05:34
  • And based on your edit the `String.cshtml` template makes no sense at all (what is the point of calling `.ToString()` on a `string`) –  Jan 27 '18 at 05:38
  • I just added the Template for the "string" class. I just want to know why this error occurs. – Andres Camacho Jan 27 '18 at 05:40
  • Read [this Q/A](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) to understand why it occurs. And NEVER use capital M `Model => Model.someProperty` as the expression (just add a `
    @Model.someProperty
    ` in your view to understand why)
    –  Jan 27 '18 at 05:42
  • I already read the publication but I still do not understand why this happens and in fact it only happens when I create a template for the String class – Andres Camacho Jan 27 '18 at 07:17
  • I would start by commenting out all the `@Html.DisplayFor(....)` in your template and test it, and start adding back one at a time to confirm exactly what is causing the error. –  Jan 27 '18 at 07:31

0 Answers0