简体   繁体   English

mvc5 icollection始终为null

[英]mvc5 icollection always null

The problem is very simple :) 问题很简单:)

I have two models Parent and Child. 我有两个模型Parent和Child。 Parent model contains collection of children. 父模型包含子项的集合。 The point is that I'm able to see children in view, but I'm not able to save changes to them. 关键是我可以看到孩子,但无法保存对他们的更改。 I spent many hours for tying to solve this problem. 我花了很多时间来解决这个问题。 Any help will be veeeery appreciate. 任何帮助将受到veeeery的赞赏。

parent model 父模型

 namespace WebApplication1.Models
{
    public class Parent
    {
        //public Parent()
        //{
        //    Child = new Collection<Child>();
        //}

        public int ParentId { get; set; }
        public string ZmiennaParent1 { get; set; }

        public ICollection<Child> Child { get; set; }
    }
}

Child model : 子模型:

    namespace WebApplication1.Models
{
    public class Child
    {
        public int ChildId { get; set; }
        public string ZmiennaChild1 { get; set; }

        public int ParentId { get; set; }
        public virtual Parent Parent { get; set; }
    }
}

my view : 我的观点 :

@for (int i = 0; i < Model.Child.Count(); i++)
{
    @Html.EditorFor(model => Model.Child.ElementAt(i).ZmiennaChild1, new { htmlAttributes = new { @class = "form-control" } })
}

and my controller : 和我的控制器:

// GET: /Parent/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); // // GET:/ Parent / Edit / 5 public ActionResult Edit(int?id){if(id == null){返回新的HttpStatusCodeResult(HttpStatusCode.BadRequest); } }

    var parent = db.Parents.Include(u => u.Child).SingleOrDefault(u => u.ParentId == id);

    if (parent == null)
    {
        return HttpNotFound();
    }
    return View(parent);
}

// POST: /Parent/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
//public ActionResult Edit(Parent parent)
public ActionResult Edit([Bind(Include="ParentId,ZmiennaParent1, Children")] Parent parent)
{
    if (ModelState.IsValid)
    {
        if (parent.Child != null)
        {
            foreach (Child element in parent.Child)
            {
                db.Entry(element).State = EntityState.Modified;
            }
            db.SaveChanges();
        }

        db.Entry(parent).State = EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    return View(parent);
}

I'll be very appreciate for any help :/ As i wrote the point is that I'm able to display related children but not able to edit them. 我将不胜感激://我写的重点是,我能够显示相关的子级,但无法对其进行编辑。

There is no Children property on Parent, there is a Child property. 父级没有子级属性,子级有子级属性。 But the Binding attribute says Children. 但是Binding属性表示Children。 Change the attribute to... 将属性更改为...

public ActionResult Edit([Bind(Include="ParentId,ZmiennaParent1, Child")] Parent parent)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM