c# - 为什么我的视图将空数据发布到控制器
问题描述
我有 List 从我的索引控制器传递到视图,它显示得很好。但是当我单击保存以发布数据时。它不会在我的视图模型中绑定数据。参见图。以下
控制器 - ActionResult Index()
var user = HttpContext.User as ClaimsPrincipal;
int PatientID = data_Patient.getPatientID(user.FindFirst(c => c.Type == ClaimTypes.Email).Value);
var patientDashboardSetting = db.PatientDashboardSetting.Include(p => p.DashboardContent).Include(p => p.Patient);
PatientDashboardSettingView model = new PatientDashboardSettingView();
model.PatientDashboardSettings = new List<PatientDashboardSetting (patientDashboardSetting);
保存控制器
[HttpPost]
public ActionResult SaveChanges(PatientDashboardSettingView patientDashboardSettings)
{
return RedirectToAction("Index", "PatientDashboardSetting");
}
查看模型
public class PatientDashboardSettingView
{
public List<PatientDashboardSetting> PatientDashboardSettings { get; set; }
}
患者仪表板模型
public partial class PatientDashboardSetting
{
public int ID { get; set; }
public int PatientID { get; set; }
public int DashboardContentID { get; set; }
public bool IsActive { get; set; }
public bool IsSharable { get; set; }
public virtual DashboardContent DashboardContent { get; set; }
public virtual Patient Patient { get; set; }
}
查看表格
@using (Html.BeginForm("SaveChanges", "PatientDashboardSetting", FormMethod.Post))
{
for (int i = 0; i < Model.PatientDashboardSettings.Count; i++)
{
<div>
@Html.HiddenFor(modelItem => Model.PatientDashboardSettings[i].ID)
@Html.HiddenFor(modelItem => Model.PatientDashboardSettings[i].PatientID)
@Html.HiddenFor(modelItem => Model.PatientDashboardSettings[i].DashboardContentID)
@Html.HiddenFor(modelItem => Model.PatientDashboardSettings[i].IsSharable)
@Html.HiddenFor(modelItem => Model.PatientDashboardSettings[i].IsActive)
</div>
}
<input type="submit" value="Submit" />
}
解决方案
您必须传递模型类型中的所有值。如果一个不在 hiddenFor 列表中,那么直到通过。
推荐阅读
- r - R中DCF模型的蒙特卡罗模拟
- php - Wordpress 自定义主题页脚中的脚本
- java - k-window中相邻元素的最长公共子序列
- android - 根据 Id 删除 ObjectBox 中的数据
- python - 使用哪些参数来提高将数据帧写入 Parquet 文件的性能?
- php - 我怎样才能得到信息的价值?
- javascript - 如何在 Promise 函数 JavaScript 中推送数组中的项目
- javascript - 如何为 mxGraph 顶点设置 box-sizing 模型?
- jenkins - Jenkins 流水线模板 - 方法
- c++ - 奇数和子数组方法不适用于大型数组?