but this Dictionary requires a model item of type 'System.Collections.Generic.Ienumerable`1[Webapplication4.Models.modelExemplo]

Asked

Viewed 812 times

2

I can not carry out enumerate, I switched to another reception of information in the View but I do not know how to correct, 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>
  • Why you declared Stringbuilder inside your builder ?

  • Because I need to instantiate the Stringbuilder object in order to use it because if I try to insert and have not instantiated it will give Nullreference

  • Instantiate yes, but do not define it within the same...

  • @Matheus, there is another way because unknown, that is not inside a constructor is an instance in the attribute, I could do so too: public Stringbuilder list { get; set; } public modelExemplo(){ list = new Stringbuilder; }

  • My mistake... disregard

  • @Matheus no problem, rs

Show 1 more comment

2 answers

0


Your view is typed for a IEnumerable<WebApplication4.Models.modelExemplo>, that is, several modelExemplo and in your Return you send only one. Change your view to @model WebApplication4.Models.modelExemplo which will work. If you need a list, please enter a list in your controller and return it;

To return the list (Obs.: keep the IEnumerable in the @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); 
  • This displays this: foreach statement cannot Operate on variables of type 'Webapplication4.Models.modelExemplo' because 'Webapplication4.Models.modelExemplo' does not contain a public Definition for 'Getenumerator'

  • If you send only one guy instead of the list you were sending, you won’t be able to use foreach, right? rs Foreach is used in list and not in 1 object only, and its "Model" there represents the "@using" that is no longer a list. To use the list, do it the way it is up there

  • Why do you create a stringbuilder? when what I think you want to create is a list of strings?!

  • I didn’t create stringbuilder, it was @rock.ownar.

0

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

Now a note: if your goal is simply to move that list and nothing else into the view, you don’t even have to create a model for that, you could just create the list in the controller and send the list, ai no using da view List object only and in foreach you simply used Model instead of 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.