所以我试图将我的Razor页面传递给一个对象列表,然后让用户为一个字段输入一个值并将其传递回控制器以在数据库中更新,但是当它出现时#39 ; s传回给控制器我刚刚变为空 这也是处理这个问题的好方法吗?注意:永远不会有大量的数据 模型
public class SessionViewModel
{
public List<Session> Sessions{ get; set; }
}
public class GroupFitnessSession
{
public string GroupFitnessSessionId { get; set; }
public string GroupFitnessName { get; set; }
public int SessionSize {get ; set;}
public bool IsConfirmed {get ; set;
}
控制器
// GET
public ActionResult ConfirmSessions()
{
var model = new SessionViewModel();
model.Sessions= unitOfWork.SessionsRepository.Get().ToList();
return View(model);
}
// POST
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ConfirmSessions(SessionViewModel model)
{
foreach (var item in model.GroupFitnessSessions)
{
item.IsConfirmed = true;
unitOfWork.GroupFitnessSessionRepository.Update(item);
}
unitOfWork.Save();
return View();
}
剃刀
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table class="table table-striped" style="margin-top: 25px;">
@foreach (var item in Model.GroupFitnessSessions)
{
@Html.DisplayFor(modelItem => item.GroupFitnessName)
@Html.EditorFor(modelitem => item.SessionSize)
}
</table>
<input type="submit" value="Create" class="btn btn-block btn-default btn-width-1" />
}