插入行mvc时插入集合

本文关键字:插入 集合 mvc | 更新日期: 2023-09-27 18:05:41

只是我想在表中插入每个员工的电话号码的集合。我知道如何在UI(客户端java脚本)中做到这一点,我需要一个特定的代码(当我在客户端增加电话数量时,从电话类型增加对象的大小

i read this:

http://www.itorian.com/2013/04/nested-collection-models-in-mvc-to-add.html

一切都很好,但是当我增加UI中的电话数量超过两个时,只在数据库中插入2行,因为:我的入口点

public ActionResult Add()
{
    var Employee = new employee();
    Employee.CreatePhoneNumbers(2);
    return View(Employee);
}
[HttpPost]
public ActionResult Add(employee emp) {
    if (ModelState.IsValid) {
        telephoneEntities db = new telephoneEntities();
        foreach(phon phone in emp.phons.ToList()) {
            if (phone.deletephon == true) {
                emp.phons.Remove(phone);
            }
        }
        db.employees.Add(emp);
        db.SaveChanges();
        return RedirectToAction("Index", "Home");
    }
    return View();
}

我需要javascript改变这个值2或3 ....(UI中添加的电话号码)

谢谢

插入行mvc时插入集合

你不需要。

Employee.CreatePhoneNumbers(2);

上面这行只是在第一次呈现视图时设置电话号码框的起始数量。如果你在UI中添加更多的电话号码字段,并保存记录,这一切都将得到照顾。

您是否进入代码以查看传递给New方法的模型中是否有超过2个电话号码?

你已经阅读并完成了教程的第二部分和第三部分了吗?