Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp mvc 4 model binding with post method

im looking for a way (if there's any) to make the model binding process to work with a List of objects, on a post method. I can easily display the list. But when it comes to the user upload a "Bien" with a post method i cant make the framework bind each list element correctly. You can see the page part where i try to make labels and text boxes is commented, cause i keep getting exceptions, so i guess im doing it wrong and i cant find a way to upload a variable amount of parameters on my model.

This is the Model:

 public class BienModel
    {

        [Required]
        [Display(Name = "Nombre del Bien.")]
        public String Nombre { set; get; }


        public List<Atributo> Atributos { set; get; }


    }

    public class Atributo
    {
        [Required]
        public String nombre { set; get; }

        [Required]
        public String valor { set; get; }
        public Atributo(){}
        public Atributo(String n, String v){
            this.nombre=n;
            this.valor=v;
        }


    }

And this is the page:

@using (Html.BeginForm()) {


    <fieldset>
        <legend>Publicar bien form.</legend>
        <ol>
            <li>
                @Html.LabelFor(m => m.Nombre)
                @Html.TextBoxFor(m => m.Nombre)
                @Html.ValidationMessageFor(m => m.Nombre)
            </li>
            @*@for (int i = 0; i < Model.Atributos.Count;i++ )
            {

                @Html.Label(Model.Atributos[i].nombre)
                @Html.TextBoxFor(m => m.Atributos[i].valor)
                @Html.ValidationMessageFor(m => m.Atributos[i].valor)

            }*@

        </ol>
        <input type="submit" value="Publicar!" />
    </fieldset>

}

And this the controller

public ActionResult PublicarBien()
    {

        //Pido a la logica los atributos

        BienModel b = new BienModel();

        b.Atributos = new List<Atributo>();
        b.Atributos.Add(new Atributo("Atributo1", ""));
        b.Atributos.Add(new Atributo("Atributo2", ""));
        b.Atributos.Add(new Atributo("Atributo3", ""));
        b.Atributos.Add(new Atributo("Atributo4", ""));


        return View(b);
    }

    [HttpPost]
    public ActionResult PublicarBien(BienModel b)
    {
        try
        {
            if (ModelState.IsValid)
            {
                //se da de alta el bien

                return RedirectToAction("Index", "Home");
            }
        }
        catch(Exception ex){
            ModelState.AddModelError("",ex.Message);
        }

        return View(b);
    }
like image 799
Cristiano Coelho Avatar asked Apr 25 '26 10:04

Cristiano Coelho


1 Answers

Your code seems fine. If the controller action you are posting to takes a BienModel as action parameter then binding should work fine:

[HttpPost]
public ActionResult SomeAction(BienModel model)
{
    ...
}

You might also take a look at the following article about the standard convention in ASP.NET MVC for binding to a list.

Also currently you only have an input field for the valor property. The nombre property doesn't have a corresponding input field so you will never get its value back. If you want that to happen you could use a hidden field:

@for (int i = 0; i < Model.Atributos.Count;i++ )
{
    @Html.LabelFor(x => x.Atributos[i].valor, Model.Atributos[i].nombre)
    @Html.HiddenFor(m => m.Atributos[i].nombre)
    @Html.TextBoxFor(m => m.Atributos[i].valor)
    @Html.ValidationMessageFor(m => m.Atributos[i].valor)
}
like image 112
Darin Dimitrov Avatar answered Apr 28 '26 01:04

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!