Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass array of objects as hidden field value into mvc controller

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?

like image 785
FrenkyB Avatar asked Mar 24 '16 22:03

FrenkyB


People also ask

How do you pass an array into a hidden field?

You can do it like this: <input type="hidden" name="result" value="<? php foreach($postvalue as $value) echo $postvalue.","; ?>">

Is it possible to pass a model object to a view from a controller?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

What is hidden field in MVC?

Here we will learn how to create or use hidden fields in asp.net mvc with a simple example using HTML helpers. Before going in-depth, let's understand the definition of hidden fields. The hidden fields are controls that allow us to store data or information on a page without displaying it.


2 Answers

@using (Html.BeginForm("Test", "Home", FormMethod.Post))
{
    var users = new List<User> 
    {
        new User { Name = "John", TotalPayed = 0, TotalDebt = 0 },
        new User { Name = "George", TotalPayed = 0, TotalDebt = 0 },
        new User { Name = "Simon", TotalPayed = 0, TotalDebt = 0 },
        new User { Name = "Antonio", TotalPayed = 0, TotalDebt = 0 }
    };

    <input name="celo" type="hidden" value="994" />
    <input name="pispis" type="hidden" value="Došel je prošel pisani Vuzem!" />

    @for(int i = 0; i < users.Count; i++)
    {
        var user = users[i];

        <input name="userji[@i].Name" type="hidden" value="@user.Name" />
        <input name="userji[@i].TotalPayed" type="hidden" value="@user.TotalPlayed" />
        <input name="userji[@i].TotalDebt" type="hidden" value="@user.TotalDebt" />
    }

    <input type="submit" value="Submit test"/>
}
like image 87
thiag0 Avatar answered Sep 30 '22 14:09

thiag0


@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
}
like image 28
Mike Wallace Avatar answered Sep 30 '22 16:09

Mike Wallace