but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable'1 [WebApplication4.Models.modelExample]

1

I can not perform enumeration, I have already switched to another information reception in the View but I do not know how to correct it, I checked other forms in StackOverflow but I did not understand.

Model:

namespace WebApplication4.Models
{
    public class modelExemplo
    {
        public StringBuilder lista { get; set; } = new StringBuilder();

    }
}

Controller:

namespace WebApplication4.Controllers{

    public ActionResult funcaodeteste()
    {               
        modelExemplo obj = new modelExemplo();
        obj.listNewsletter.AppendLine("teste1");
        obj.listNewsletter.AppendLine("teste2");

        return View(obj);       
    }

}

View:

@model IEnumerable<WebApplication4.Models.modelExemplo>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Download</title>
</head>
<body>



    @foreach (var item in Model) {
        <p>item</p>
    }


</body>
</html>
    
asked by anonymous 25.10.2017 / 15:06

2 answers

0

Your view is typed into IEnumerable<WebApplication4.Models.modelExemplo> , ie multiple modelExemplo and on your return you only send one. Change in your view to @model WebApplication4.Models.modelExemplo that will work. If you need a list, instantiate a list in your controller and return it;

To return the list (obs .: keep IEnumerable in @model ):

IEnumerable<modelExemplo> lista = new List<modelExemplo>();
modelExemplo obj = new modelExemplo();
obj.listNewsletter.AppendLine("teste1");
obj.listNewsletter.AppendLine("teste2");
lista.Add(obj)  
return View(lista); 
    
25.10.2017 / 15:28
0

As I said before, I think what you want to do is pass a list of strings, right? within your model object, you create the list and then go through the list ..

Now a note: if your goal is simply to pass this list and nothing else for the view, nor do you need to create a model for it, you could simply create the list in the controller and send the list, the List object and in foreach you simply used the Model instead of the Model.list

MODEL

 namespace WebApplication4.Models
{
    public class modelExemplo
    {
        public List<string> lista { get; set; };

    }
}

CONTRLLER:

namespace WebApplication4.Controllers{

    public ActionResult funcaodeteste()
    {               
        modelExemplo obj = new modelExemplo();
        obj.lista = new List<string>();

         obj.lista.Add("teste1");
        obj.lista.Add("teste2");

        return View(obj);       
    }

}

VIEW

@model WebApplication4.Models.modelExemplo

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Download</title>
</head>
<body>



    @foreach (var item in Model.lista) {
        <p>item</p>
    }


</body>
</html>
    
06.11.2017 / 12:06