Pass a set of objects as a hidden field value in the mvc controller

advertisements

I am using VS2015, MVC5. My submit form:

        @using (Html.BeginForm("Test", "Home", FormMethod.Post))
    {
        <input name="celo" type="hidden" value="994" />
        <input name="pispis" type="hidden" value="Došel je prošel pisani Vuzem!" />
        <input name="userji" type="hidden"
            value="[{'ID':1,'Name':'John','TotalPayed':0,'TotalDebt':0},
                {'ID':2,'Name':'George','TotalPayed':0,'TotalDebt':0},
                {'ID':3,'Name':'Simon','TotalPayed':0,'TotalDebt':0},
                {'ID':4,'Name':'Antonio','TotalPayed':0,'TotalDebt':0}]" />
        <input type="submit" value="Submit test"/>
    }

Controller:

    public void Test(int celo, string pispis, List<User> userji)
    {

    }

Class User:

    public class User
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int TotalPayed { get; set; }
    public int TotalDebt { get; set; }
}

Values 'celo' and 'pispis' are transferred correctly. I have a problem with array of objects 'userji' - it is null on controller. Is there a way to pass array of objects into controller using submit form?


@FrenkyB You could change the binding to be a string and then digest it into a list like this.

public void Test(int celo, string pispis, string userji)
{
    var myList = JsonConvert.DeserializeObject<List<User>>(userji);

    //stuff
}