首页 > 解决方案 > 为什么我的视图将空数据发布到控制器

问题描述

我有 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" />
}

标签: c#asp.netasp.net-mvc

解决方案


您必须传递模型类型中的所有值。如果一个不在 hiddenFor 列表中,那么直到通过。


推荐阅读