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?
You can do it like this: <input type="hidden" name="result" value="<? php foreach($postvalue as $value) echo $postvalue.","; ?>">
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.
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.
@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"/>
}
@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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With