0

My issue is the following:

I can not send my data for display in my view.

First, you must know that I connect to an external Filemaker DB and that the "results" variable retrieves this data.

"results" is of type IEnumerable .

For the recovery part, results contains my model that he found.

But it seems that I'm missing a piece of code because my view does not show any data.

Indeed, my goal is to send existing data into a form.

Could you help me?

Controller

   [Authorize]
    public ActionResult Index()
    {
        //HttpContext.Session.SetInt32("idMember", 26);

        List<Models.Members> list = new List<Models.Members>();
        Models.Members m = new Models.Members();
        m.Member_NameFirst = "test1";
        Models.Members m2 = new Models.Members();
        m2.Member_NameFirst = "test2";
        list.Add(m);
        list.Add(m2);
        IEnumerable<Models.Members> members = list;
        return View(list);
    }

View

@model IEnumerable<Members>


@foreach (var item in Model)
{
    @Html.DisplayFor(o => item.Member_NameFirst)
}
Korpin
  • 193
  • 16
  • If **results" is of type IEnumerable**, why is your view not strongly typed to a collection type ? Are you getting an error now ( about the passed data type not matching than what the view is expecting) ? What is happening now ? – Shyju Nov 23 '18 at 16:33
  • I only receive this error: jquery.js:9600 GET https://localhost:44338/Members/GetMember/ 500 – Korpin Nov 23 '18 at 16:39
  • That means your `GetMember` method is crashing. Check the response tab of the ajax call to see the details/ put a breakpoint in your method and see which line is crashing. – Shyju Nov 23 '18 at 16:41
  • it crashes when he hit : xhr.send( options.hasContent && options.data || null ); Dont know how to solve it :/ – Korpin Nov 23 '18 at 16:57
  • Ofc you are right! So i guess i need to declare to my view: @model List ? – Korpin Nov 23 '18 at 17:28

1 Answers1

2

Something like this for your result:

@model IEnumerable<BlockedIPViewModel>

Ref. How to pass IEnumerable list to controller in MVC including checkbox state?

This is my successful example.

Class:

namespace Test.Models
{
    public class Members
    {
        public string Member_NameFirst { get; set; }
    }
}

Controller:

public ActionResult Index()
{
    // Test data
    List<Members> list = new List<Members>();
    Members m = new Members();
    m.Member_NameFirst = "test1";
    Members m2 = new Members();
    m2.Member_NameFirst = "test2";
    list.Add(m);
    list.Add(m2);
    IEnumerable<Members> members = list;
    return View(list);
}

[Index] View:

@model IEnumerable<Test.Models.Members>

@foreach (var item in Model)
{
    @Html.DisplayFor(o => item.Member_NameFirst)
}

Be careful of your view @model as well.

Wing Kui Tsoi
  • 325
  • 4
  • 15