0

I need to display a specific form in a view with a model. The model is like that:

This is the final object filled what I need :

public class ObjetTotal
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Numero { get; set; }
        public string Value { get; set; }
    }

I choose to cut the form into two differents parts :

  • First a "static" part where the user can put common values for the differents ObjetsTotal.
  • Second a "variable" part where the user put differents values for the differents ObjetsTotal.

The final aim is that the user doest'n have to type, the same thing for all the objects ObjetTotal.

So, I create other objects (I don't know if it's a good practice) which represents the differents part of the form.

The static part with MainObjet and the variable part with Numbers. I put these two object into an other object "Mix" which contains one "MainObjet" and a list of "Numbers".

public class MainObjet
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

public class Numbers
{
    public string Numero { get; set; }
    public string Value { get; set; }
}

public class Mix
{
    public MainObjet obj { get; set; }
    public IEnumerable<Numbers> num { get; set; }

    public Mix()
    {
       obj = new MainObjet();
       num = new List<Numbers>();
    }
}

Then I want to render the model Mix in a view to have the two parts of the form.

I've try this :

@model App.Models.Mix

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Mix</legend>

    <h3>First Properties</h3>
    <div>
        @Html.TextBoxFor(model => model.obj.Id);
        @Html.TextBoxFor(model => model.obj.Name);
        @Html.TextBoxFor(model => model.obj.Description);
    </div>
    <div>
        <table>
            @for (int i = 0; i < 5; i++)
            { 
                <tr>
                    <td>
                        @Html.TextBoxFor(model => model.num[i].Numero)
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model.num[i].Value)
                    </td>
                </tr>
            }
        </table>
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

Viewtest

But after the submit I get an object Mix null in this ActionResult :

[HttpPost]
public ActionResult Test(Mix obj)
{
    return View();
}

Can you explain me how to do that ? May I'm on a wrong way.

Don't consider the design of the form, and I don't know the right type to put to Numbers, Maybe a simple list be enough for that.

Julien698
  • 676
  • 3
  • 10
  • 27
  • Because the parameter in your post method is named the same as one of your properties. Change it to anything else and it will bind fine. –  Dec 28 '17 at 21:41

1 Answers1

0

The thing I can see is that you are missing the initialization of your model properties in the parameter-less constructor. You should try to update your model code to be:

public class Mix
{
    public MainObjet obj { get; set; }
    public IEnumerable<Numbers> num { get; set; }

    public Mix()
    {
        obj = new MainObjet();
        num = new List<Numbers>();
    }
}

As the model binder will instantiate your model, and it will find obj and num to null and will not be able to post the values back.

Hope this helps you.

Patrick
  • 1,717
  • 7
  • 21
  • 28
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • I update my code, I get the object in the httppost, it's not null but the object is empty although I type some datas in the form. – Julien698 Dec 28 '17 at 13:46
  • your code looks good to me, this should work IMO – Ehsan Sajjad Dec 28 '17 at 14:00
  • Sadly, I get an empty object in Httppost, I don't understant why... – Julien698 Dec 28 '17 at 14:18
  • you can try to validate the html which was generated from the mvc, the framework is bingindg each input by it's name for example if you have array of property X it will generate two inputs x[0] and x[1], so maybe in the names is the issue – Petar Minev Dec 29 '17 at 06:56