我有一个似乎无法解决的问题
我有以下模型设置:
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; }
}
在我的视图中显示如下:
@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 })
}
}
我也在渲染模型的其余部分,但这并不重要,因为它正常工作。为了测试,这是我接受帖子的方法:
[HttpPost]
public JsonResult SubmitLog(LogModel log)
{
return Json(log);
}
然后,一旦我按下提交按钮,该方法的响应是:
{"Log":[],"Winner":{"ID":1,"Name":"Blues","Image":null},"Runnerup":{"ID":14,"Name":"Stormers","Image":null}}
日志列表总是空的,我不知道为什么。因为如果我查看firebug上的POST选项卡,我可以看到所有发布的值:
%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
修改
我找到了一个有效的奇怪解决方案,但我不确定它是否理想:
[HttpPost]
public JsonResult SubmitLog(List<MyPredictor.Models.Log.LogItemModel> log, LogModel model)
{
model.Log = log;
return Json(model);
}
答案 0 :(得分:0)
尝试
[HttpPost]
public JsonResult SubmitLog(List<MyPredictor.Models.Log.LogItemModel> log)
{
return Json(log);
}
假设您的阵列在表单中正确设置,绑定模型必须是POCO(Plain Old C#Object)才能使默认绑定器正常工作。
答案 1 :(得分:0)
只需替换
@model List<MyPredictor.Models.Log.LogItemModel>
与
@model MyPredictor.Models.Log.LogModel
然后在for
:
@for (int i = 0; i < Model.Log.Count; i++)
hiddens:
@Html.HiddenFor(m => m.Log[i].SomeProperty ...
但是,我仍然不知道Winner
和Runnerup
属性在哪里。
答案 2 :(得分:0)
请按照本文明确解释模型绑定并帮助您正确绑定list
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
关键是设置正确的索引
<input type="hidden" name="Log.Index" value="cold" />
<input type="hidden" name="Log[cold].TeamID " value="your value" />
<input type="hidden" name="Log[cold].TeamName " value="your value" />
此处cold
是列表的索引,您可以将任何值设置为索引,但它应该是唯一的。