【发布时间】:2015-01-05 21:10:55
【问题描述】:
我有这个类作为我的模型
public class Forecast
{
public List<Test> Tests { get; set; }
public TimeGranularities Granularity { get; set; }
public DateTime StartTime { get; set; }
public string StartTimeString { get; set; }
public int StartTimeMonth { get; set; }
public int StartTimeYear { get; set; }
public DateTime EndTime { get; set; }
public string EndTimeString { get; set; }
public int EndTimeMonth { get; set; }
public int EndTimeYear { get; set; }
public List<ForecastItem> ForecastItems { get; set; }
}
这是用于测试的类
public class Test
{
public int Testint { get; set; }
public decimal Testdec { get; set; }
public bool Testbool { get; set; }
}
我的 HTTPGET 操作结果在控制器中
[Authorize]
[HttpGet]
public ActionResult Index()
{
var model = new Forecast
{
StartTimeString = DateTime.Now.AddMonths(-1).ToShortDateString(),
EndTimeString = DateTime.Now.ToShortDateString(),
Tests = new List<Test>
{
new Test{Testint = 1, Testbool = false, Testdec = 4},
new Test{Testint = 2, Testbool = true, Testdec = 5},
new Test{Testint = 3, Testbool = false, Testdec = 6}
}
};
return View(model);
}
我的看法
@model ......Forecast
@using (Ajax.BeginForm("Index", "Forecast", new AjaxOptions { UpdateTargetId = "ForecastContainer", InsertionMode = InsertionMode.Replace, HttpMethod = "Post", OnSuccess = "calcTotals", LoadingElementId = "LoadingContainer" }))
{
<div>
<div style="display: table-cell">
<input type="submit" value="Generate" class="btn btn-default" />
</div>
</div>
<br />
<div>
@foreach (var t in Model.Tests)
{
@Html.EditorFor(modelItem => t.Testint)
@Html.EditorFor(modelItem => t.Testbool)
@Html.EditorFor(modelItem => t.Testdec)
<br/>
}
</div>
视图也显示了我想要的。但是当我提交表单并在控制器中的 POST 操作结果中检查返回的模型时,model.Tests 不仅不保存我的更改,它还为空。
我这样做是否正确。
【问题讨论】:
-
为什么在你的视图中转换你的列表模型中的 vievag var 之后你不返回你的模型列表在 vievag 中
-
你应该试试 for-loop 而不是 foreach。这里有很长的解释:stackoverflow.com/questions/8894442/…
标签: c# asp.net-mvc model