View returning empty model to Controller

I'm current working on making a sort of wiki of plants. The first thing we started doing is the managment of the users in the site. For that we made this view that shows the results of a search from the database. The search works fine and the ViewData["datos"] returns what it should, and in this table we show the users (From the Model Usuario). In the end, for each user it should show a button that sends the Model for that user to another Controller. The problem is that the Controller receives an empty model. Here's the code for the View. The input with the value Editar is the one that should send the model.

@model AtlasPlantasMedicinales.Models.Usuario
@{
    ViewBag.Title = "Tabla";
}

@if (ViewData["datos"]!=null){ 

        <table>
        @foreach (var elemento in (List<AtlasPlantasMedicinales.Models.Usuario>)(ViewData["datos"]))
        {
            <tr>
                <td>
                    @elemento.Nombre
                </td>
                <td>
                    @elemento.Carrera
                </td>
                <td>
                    @elemento.Institucion
                </td>
                <td>
                    @elemento.usuario
                </td>
                <td>
                    @elemento.correo
                </td>
                @using (Html.BeginForm("FormularioEditar", "Usuario", FormMethod.Post))
                {
                    <td>@Html.HiddenFor(m => elemento) <input type="submit" value="Editar"></td>
                }
            </tr>
        }
        </table>
}

How can we send the model in elemento to the controller?

We have also tried making a Html.HiddenFor for each attribute in the model, but it still doesn't work. Please keep in mind that me (and my teammmates) are completely new at ASP.NET MVC 4

Upvotes: 0

Views: 785

Answers (1)

Aboodz
Aboodz

Reputation: 1584

This is a model binding problem. If you are willing to use the default model binder, use for loop instead of foreach. Your code will go something as as follows (Since you didn't post the Model/ViewModel, I will use ModelId as the property, and usuario as the value):

@for (int i = 0; i < (List<AtlasPlantasMedicinales.Models.Usuario>)(ViewData["datos"])).Count(); i++)
{
    var elemento = (List<AtlasPlantasMedicinales.Models.Usuario>)(ViewData["datos"])).ElementAt(i);

            <tr>
                <td>
                    @elemento.Nombre
                </td>
                <td>
                    @elemento.Carrera
                </td>
                <td>
                    @elemento.Institucion
                </td>
                <td>
                    @elemento.usuario
                </td>
                <td>
                    @elemento.correo
                </td>
                @using (Html.BeginForm("FormularioEditar", "Usuario", FormMethod.Post))
                {
                    <td>@Html.HiddenFor(m => m.ModelId, new { @Value = elemento.usuario }) <input type="submit" value="Editar"></td>
                }
            </tr>
        }

Now, in the controller you will receive Usuario model with only the ModelId property initialized. For more information about the model binding, I recommend you to this read this blog post by Scott Hanslman

Upvotes: 1

Related Questions