ASP .NET Core: View grid of multiple items to be edited

I'm trying to construct a view where the records from a table (machines) get uploaded and that each one can be edited in a view.

This is my Get method:

public async Task<IActionResult> Test()
        {

            return View(await _context.Machines.Include(t => t.MachineTypes).Include(p => p.Suppliers).AsNoTracking().ToListAsync());
        }

Pretty simple for what I need. I'm getting a list of the table Machines and including the related tables: MachineTypes and Suppliers because this information should be able to be edited in the view.

Now, with the view I'm quite lost, sorry for this.

I started defining the model to be used:

@model IEnumerable<Application.Models.Machine>

Question[Answered] Is correct to use an IEnumerable? Answer by uni: Yes

Then, as seen in some examples, I use a Html.BeginForm to indicate the controller and action:

@using (Html.BeginForm("Machines","TestPost"))

Sidenote: The Post method is not defined yet so I'm using any name for now

Then comes the iteration to read every element of the list:

    @Html.ValidationSummary(true)
    @for(int i = 0; i < Model.Count() ; i++)

Question[Answered]: In this part I'm not sure if 'Model.Machine.Count()' is correct. Answer by uni: It's correct. No need to specify 'Machine'

Inside the iteration I don't know how to code to show the MachineName (MchName) to begin with. I'm doing this:

    @for(int i = 0; i < Model.Count() ; i++)
    { 
        <tr>
            <td>
                @Html.HiddenFor(modelItem => modelItem.Machine[i].Id)
                @Html.DisplayNameFor(model => model.MchName)
            </td>
        </tr>
     }

Question[Answered]: In this part:

    @Html.HiddenFor(modelItem => modelItem.Machine[i].Id)

I don't know why I need to use the HiddenFor, I saw it on another example in the forum. Answer by uni: This is one of many ways to send the values to the controller.

Question[Answered]: Also, this part

@Html.HiddenFor(modelItem => modelItem.Machine[i].Id) 

Answer by uni: 'Machine' don't need to be included, same as before

Continuation of the View

For the iteration part, since 'Machine' don't need to be included...

[NEW]Question: How should I call the item wanted from the Machine table?

I've got this:

    @for(int i = 0; i < Model.Count() ; i++)
    { 
        <tr>
            <td>
            @Html.HiddenFor(modelItem => i.Id)
            @Html.DisplayNameFor(modelItem => model.MchName)
            </td>
        </tr>

     }

Both of this are wrong, but I was trying to figure out how to show the item inside the iteration. I wonder if, since I'm using an IEnumerable, it would be best if I use a @foreach instead of a for();

Thanks

Upvotes: 2

Views: 128

Answers (1)

univ
univ

Reputation: 737

Question: Is correct to use an IEnumerable?

Answer: Yes

Question: In this part I'm not sure if 'Model.Machine.Count()' is correct, and well, I'm getting the error: IEnumerable does not contain a definition for Machine. Is it enough if I put it this way?: @for(int i = 0; i < Model.Count() ; i++)

Answer: You already have a collection of Machines (IEnumerable<Application.Models.Machine>) which is stored in your model so Model.Count() would be the correct way to go as each Machine model doesn't have a child Machine object

Question: @Html.HiddenFor(modelItem => modelItem.Machine[i].Id) I don't know why I need to use the HiddenFor, I saw it on another example in the forum.

Answer: When you do an HTTP post you have many ways to send values to the controller action. Hidden fields are a way to do that. In this case you are sending the id of the machine as part of your form submission.

Question: @Html.HiddenFor(modelItem => modelItem.Machine[i].Id) is showing the same error: IEnumerable does not contain a definition for Machine. I tough I should mention the table.

Answer: The answer to this one is the same as the answer that I mentioned above regarding the Count() invocation in the for loop.

Hope this helps!

Upvotes: 1

Related Questions