C# ASP.NETMVC-带集合、提交的模型

C# ASP.NETMVC-带集合、提交的模型,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我有一个模型看起来像 public class Patient { private ICollection<Refill> _refills = new List<Refill>(); public int Id { get; set; } public string FirstName { get; set; } public virtual ICollection<Refill> Ref

我有一个模型看起来像

public class Patient
{
    private ICollection<Refill> _refills = new List<Refill>();
    public int Id { get; set; }
    public string FirstName { get; set; }
                  
    public virtual ICollection<Refill> Refills
    {
        get { return _refills; }
        set { _refills = value; }
    }
}

public class Refill
{
    public int Id { get; set; }

    public RefillActivityStatus RefillActivityStatus { get; set; }
    public DateTime? RefillDate { get; set; }
    public RefillType RefillType { get; set; }
    public string RXNumber { get; set; }
}

但是,重新填充不会在HttpPost上传播。它只是空的,我需要做些什么来修复它呢?

用这个代替您的
foreach

@for (int i = 0; i < Model.Refills.Count(); i++)
{
    @Html.Hidden("Refills[" + i + "].Id", Model.Refills[i].Id)
    <div class="editor-label">
        @Html.Label("Refills[" + i + "].RXNumber", Model.Refills[i].RXNumber)
    </div>
    <div class="editor-field">
        @Html.Editor("Refills[" + i + "].RXNumber")
    </div>     
}
@for(int i=0;i
能否显示允许用户输入值的表单代码?我正在获取方法“System.Web.Mvc.Html.LabelExtensions.LabelFor(System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression,string)”的类型参数,但无法从用法中推断出来。尝试显式指定类型参数。对于LabelForbecouse,您需要将值映射为
Refills[ix].RXNumber
(将此设置为名称),另一种方式是,您使用的是一个助手(
EditorFor
),该助手创建了一个名为
RXNumber
的输入,这是错误的(必须是
Refills[i].RXNumber
才能正确映射)好的,谢谢,但我还发现,您仍然可以使用@Html.EditorFor(model=>model.Refills[I].RXNumber),而无需手动创建字符串。
[HttpPost]
public ActionResult Details(Patient patient)
{
    Patient p = db.Patients.Find(patient.Id);
    db.Entry(p).State = EntityState.Modified;
    db.Entry(p).CurrentValues.SetValues(patient);
    // Would include Refill changes too but it doesn't
    db.SaveChanges();

    return View(p);
}
@for (int i = 0; i < Model.Refills.Count(); i++)
{
    @Html.Hidden("Refills[" + i + "].Id", Model.Refills[i].Id)
    <div class="editor-label">
        @Html.Label("Refills[" + i + "].RXNumber", Model.Refills[i].RXNumber)
    </div>
    <div class="editor-field">
        @Html.Editor("Refills[" + i + "].RXNumber")
    </div>     
}