我有此列表,该列表是从控制器传递到视图的,但是当我尝试保存该列表时未保存任何结果,这可能是错误的吗?enter image description here @ Html.AntiForgeryToken()
@for (var i = 0; i < Model.IndicatorDetails.Count; i++)
{
<tr>
<td>
@Model.IndicatorDetails[i].FacilityID
<input name=" @Model.IndicatorDetails[i].FacilityID" value=" @Model.IndicatorDetails[i].FacilityID" />
</td>
<td>
@Model.IndicatorDetails[i].valueMeasured
<input name=" @Model.IndicatorDetails[i].valueMeasured" value=" @Model.IndicatorDetails[i].valueMeasured" />
</td>
<td>
@Model.IndicatorDetails[i].Month
<input name=" @Model.IndicatorDetails[i].Month" value=" @Model.IndicatorDetails[i].Month" />
</td>
<td>
@Model.IndicatorDetails[i].Year
<input name=" @Model.IndicatorDetails[i].Year" value=" @Model.IndicatorDetails[i].Year" />
</td>
</tr>
}
<div class="card-footer">
<input type="submit" value="Submit" class="btn btn-primary btn-sm" />
<button type="reset" class="btn btn-danger btn-sm">
<i class="fa fa-ban"></i> Reset
</button>
</div>
</form>
PostAverageWaitTime方法,这是用于保存表单的方法,但是当我调试检查“ PillarAndIndicatorViewModels”中的内容时,结果为0,但获取的内容却更多t
public ActionResult PostAverageWaitTime(PillarAndIndicatorViewModel PillarAndIndicatorViewModels)
{
try
{
DateTime EncodedDate = DateTime.Today;
foreach (var IndicatorDet in PillarAndIndicatorViewModels.IndicatorDetails)
{
IndicatorDetail ID = new IndicatorDetail
{
StrategyPillarID = IndicatorDet.StrategyPillarID,
IndicatorID = IndicatorDet.IndicatorID,
FacilityID = IndicatorDet.FacilityID,
valueMeasured = IndicatorDet.valueMeasured,
valueMeasurement = IndicatorDet.valueMeasurement,
EncodedDate = EncodedDate,
active = IndicatorDet.active,
OPIP = IndicatorDet.OPIP,
Month = IndicatorDet.Month,
Year = IndicatorDet.Year
};
db.IndicatorDetails.Add(ID);
db.SaveChanges();
}
TempData["Success"] = "Details Added Successfully";
return RedirectToAction("Indicatordetail");
}
catch(Exception ex)
{
string exp = ex.ToString();
}
return View();
}
答案 0 :(得分:0)
从不填充对象ID,因此您要添加一个空的对象。 尝试添加一个断点并按照控制器中的foreach进行操作
答案 1 :(得分:0)
MVC默认模型绑定程序通过元素的输入名称绑定,并且您正在使用foreach循环在指标详细信息对象上呈现列表 结果,多个inpupt元素将具有相同的名称,因此MVC模型联编程序将无法对其进行绑定
尝试在视图中使用for循环,以便列表中的每个元素都具有这样的唯一名称
bad