我正在使用VS2015,MVC5。 我的提交表格:
@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"/>
}
控制器:
public void Test(int celo, string pispis, List<User> userji)
{
}
班级用户:
public class User
{
public int ID { get; set; }
public string Name { get; set; }
public int TotalPayed { get; set; }
public int TotalDebt { get; set; }
}
正确传输值'celo'和'pispis'。我有一个对象数组'userji'的问题 - 它在控制器上是null。有没有办法使用提交表单将对象数组传递给控制器?
答案 0 :(得分:4)
@FrenkyB您可以将绑定更改为字符串,然后将其消化为这样的列表。
public void Test(int celo, string pispis, string userji)
{
var myList = JsonConvert.DeserializeObject<List<User>>(userji);
//stuff
}
答案 1 :(得分:4)
@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"/>
}
答案 2 :(得分:1)
我知道做你所要求的唯一方法就是作弊。我过去做的方式是作为字符串传递,然后在控制器端将数据解析为JSON。