I have a problem that I cannot seem to resolve
I have the following models setup:
public class LogModel
{
public LogModel()
{
this.Log = new List<LogItemModel>();
}
public List<LogItemModel> Log { get; set; }
public TeamModel Winner { get; set; }
public TeamModel Runnerup { get; set; }
}
public class LogItemModel : BaseModel
{
public int TeamID { get; set; }
public string TeamName { get; set; }
public int TeamPosition { get; set; }
}
Displaying it in my view as follows:
@using (Ajax.BeginForm("SubmitLog", new AjaxOptions { OnSuccess = "onSuccess" }))
{
@model List<MyPredictor.Models.Log.LogItemModel>
@for (int i = 0; i < Model.Count; i++)
{
@Html.HiddenFor(m => m[i].TeamID, new { @class = "selected_team_id", dataposition = i })
@Html.HiddenFor(m => m[i].TeamName, new { @class = "selected_team_name" })
@Html.HiddenFor(m => m[i].TeamPosition, new { @class = "selected_team_position", dataposition = i, @value = i })
}
}
I am also rendereing the rest of the model, but that is not important, as it is working correctly. For testing, this is my method to acceept the post:
[HttpPost]
public JsonResult SubmitLog(LogModel log)
{
return Json(log);
}
Then once I press the submit button, the response from the method is:
{"Log":[],"Winner":{"ID":1,"Name":"Blues","Image":null},"Runnerup":{"ID":14,"Name":"Stormers","Image":null}}
The log list is always empty and I am not sure why. Because if I look in the POST tab on firebug, I can see all the values being posted:
%5B0%5D.TeamID=14&%5B0%5D.TeamName=Stormers&%5B0%5D.TeamPosition=1&%5B1%5D.TeamID=13&%5B1%5D.TeamName=Sharks&%5B1%5D.TeamPosition=2&%5B2%5D.TeamID=6&%5B2%5D.TeamName=Crusaders&%5B2%5D.TeamPosition=3&%5B3%5D.TeamID=-1&%5B3%5D.TeamName=&%5B3%5D.TeamPosition=4&%5B4%5D.TeamID=-1&%5B4%5D.TeamName=&%5B4%5D.TeamPosition=5&%5B5%5D.TeamID=-1&%5B5%5D.TeamName=&%5B5%5D.TeamPosition=6&%5B6%5D.TeamID=-1&%5B6%5D.TeamName=&%5B6%5D.TeamPosition=7&%5B7%5D.TeamID=-1&%5B7%5D.TeamName=&%5B7%5D.TeamPosition=8&%5B8%5D.TeamID=-1&%5B8%5D.TeamName=&%5B8%5D.TeamPosition=9&%5B9%5D.TeamID=-1&%5B9%5D.TeamName=&%5B9%5D.TeamPosition=10&%5B10%5D.TeamID=-1&%5B10%5D.TeamName=&%5B10%5D.TeamPosition=11&%5B11%5D.TeamID=-1&%5B11%5D.TeamName=&%5B11%5D.TeamPosition=12&%5B12%5D.TeamID=-1&%5B12%5D.TeamName=&%5B12%5D.TeamPosition=13&%5B13%5D.TeamID=-1&%5B13%5D.TeamName=&%5B13%5D.TeamPosition=14&%5B14%5D.TeamID=-1&%5B14%5D.TeamName=&%5B14%5D.TeamPosition=15&Winner.ID=1&Winner.Name=Blues&Winner.Image=&Runnerup.ID=14&Runnerup.Name=Stormers&Runnerup.Image=&X-Requested-With=XMLHttpRequest
EDIT
I found a strange solution that works, but I am not sure it is ideal:
[HttpPost]
public JsonResult SubmitLog(List<MyPredictor.Models.Log.LogItemModel> log, LogModel model)
{
model.Log = log;
return Json(model);
}