0

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

4
  • You need a hidden field for each of the model's properties - not the whole model. MVC can't serialize the entire model into a single form field value. Commented Aug 4, 2014 at 21:30
  • You'll need the form to enclose all your properties. You should probably use @Html.DisplayEditorFor(). Commented Aug 4, 2014 at 21:35
  • We already tried that. We wrote @Html.HiddenFor(m => elemento.propriety) for each propriety but it still sends an empty model. @Ant P Commented Aug 4, 2014 at 21:37
  • Include you controllers post action method Commented Aug 5, 2014 at 2:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.