ASP.Net MVC - 模型收集,提交 [英] ASP.Net MVC - model with collection, Submit
本文介绍了ASP.Net MVC - 模型收集,提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个模型,看起来像
I have a Model that looks like
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; }
}
下面就是我要做的。我想在查看和当用户点击保存更改来填充这些。我想实体框架进行保存。我遇到的问题是在查看我这样做
Here is what I am trying to do. I would like to Populate these in the View and when the user clicks save changes. I would like Entity Framework to save. The problem I am having is in the View I do this
@foreach (var m in Model.Refills)
{
@Html.HiddenFor(model=>m.Id)
<div class="editor-label">
@Html.LabelFor(model => m.RXNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => m.RXNumber)
</div>
}
我想在控制器像这样做的。
I am trying to do in the Controller something like this
[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);
}
然而,笔芯上不HttpPost传播。这只是空的,做我需要做修复它是什么?
However, Refills doesn't propagate on HttpPost. It is just empty, What do i need to do to fix it?
推荐答案
试试这个insted的你的的foreach
:
Try this insted of your 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>
}
这篇关于ASP.Net MVC - 模型收集,提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文