在MVC中为嵌套集合添加更多项目? [英] Adding more items to nested collections in MVC?

查看:104
本文介绍了在MVC中为嵌套集合添加更多项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个模型, employees1 phone_manager Employees1 具有名为 employee_phone_manager 的I​​Collection,其中包含具有匹配员工ID的所有行。

I have 2 models, employees1 and phone_manager. Employees1 has an ICollection called employee_phone_manager that contains all the rows with the matching employee ID.

关注在线教程,我将两个模型集成在一起,以便我可以从我的创建视图中动态添加和删除项目。在我的编辑视图中,我可以编辑或删除电话号码,但不能添加。我发现的第一件事是,员工ID没有填充在动态添加的项目上,所以我修改了HTML帮助器来插入正确的员工ID,这就纠正了我对外键约束和引用完整性约束的一个问题。但是,它似乎并没有将项目插入到集合中。

Following a tutorial online, I integrated the two models together so that I could dynamically add and delete items from my create view. Inside my edit view, I can edit or delete phone numbers, but not add. The first thing I discovered was that the employee ID wasn't populating on the dynamically added items, so I modified the HTML helper to insert the proper employee ID, and this corrected an issue I was getting about foreign key constraints and referential integrity constraints. However, it appears that it doesn't actually insert the item into the collection.

这是我的编辑控制器的邮政编码:

Here is my post code for my edit controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "employee_id,assigned_id,all_id,first_name,last_name,birth_day,gender,social,address_line_1,address_line_2,city,state,zip,hire_date,termination_date,emergency_contact_name,emergency_contact_phone,notes,employee_phone_manager")] employees1 employees1)
    {
        if (ModelState.IsValid)
        {
            db.Entry(employees1).State = EntityState.Modified;
            db.SaveChanges();
            foreach (var item in employees1.employee_phone_manager)
            {
                if (item.phone_id == 0 && !item.deleted)
                {
                    db.phone_manager.Add(item);
                }
                else if (item.phone_id != 0)
                {
                db.Entry(item).State = EntityState.Modified;
                db.SaveChanges();
                }

            }
            return RedirectToAction("Index");
        }
        ViewBag.phonetype = new SelectList(db.phone_types, "phone_type_id", "phone_type_name");
        ViewBag.all_id = new SelectList(db.all_employees, "all_id", "all_id", employees1.all_id);
        return View(employees1);
    }

而我的视图

<div class="box border blue">
    <div class="box-title">
        <h4><i class="fa fa-phone"></i>Phone Number</h4>
        <div class="tools">
            <a href="javascript:;" class="collapse">
                <i class="fa fa-chevron-up"></i>
            </a>
            @Html.RemoveLink("<i class=\"fa fa-times\"></i>", "div.phoneNumber", "input.mark-for-delete")
        </div>
    </div>
    <div class="box-body">
        @Html.HiddenFor(x => x.phone_id)
        @Html.HiddenFor(x => x.employee_id)
        <div class="form-group">
            @Html.LabelFor(x => x.phone_type, new { @class = "control-label col-md-3" })
            @Html.DropDownListFor(x => x.phone_type, (SelectList)ViewBag.phonetype, new { @class = "form-control" })
        </div>
        <div class="form-group">
            @Html.LabelFor(x => x.phone_number, new { @class = "control-label col-md-3" })
            @Html.TextBoxFor(x => x.phone_number, new { @class = "phone  form-control", size = "10" })
        </div>
        <div class="form-group">
            @Html.LabelFor(x => x.phone_extension, new { @class = "control-label col-md-3" })
            @Html.TextBoxFor(x => x.phone_extension, new { size = "4", @class = "form-control" })
        </div>
        @Html.HiddenFor(x => x.date_added, new { @Value = System.DateTime.Now })
        @Html.HiddenFor(x => x.deleted, new { @class = "mark-for-delete" })
    </div>
</div>

和HTML帮助器

    public static IHtmlString AddLink<TModel>(this HtmlHelper<TModel> htmlHelper, string linkText, string containerElement, string counterElement, string collectionProperty, Type nestedType, string empID = "0")
    {
        var ticks = DateTime.UtcNow.Ticks;
        var nestedObject = Activator.CreateInstance(nestedType);
        var partial = htmlHelper.EditorFor(x => nestedObject).ToHtmlString().JsEncode();
        partial = partial.Replace("id=\\\"nestedObject", "id=\\\"" + collectionProperty + "_" + ticks + "_");
        partial = partial.Replace("id=\\\"nestedObject", "id=\\\"" + collectionProperty + "_" + ticks + "_");
        partial = partial.Replace(".employee_id\\\" type=\\\"hidden\\\" value=\\\"0", ".employee_id\\\" type=\\\"hidden\\\" value=\\\"" + empID);
        var js = string.Format("javascript:addNestedForm('{0}','{1}','{2}','{3}');return false;", containerElement, counterElement, ticks, partial);
        TagBuilder tb = new TagBuilder("a");
        tb.Attributes.Add("href", "#");
        tb.Attributes.Add("onclick", js + ";addMasks();");
        tb.Attributes.Add("class", "addNewItemsLink");
        tb.InnerHtml = linkText;
        var tag = tb.ToString(TagRenderMode.Normal);
        return MvcHtmlString.Create(tag);
    }

    private static string JsEncode(this string s)
    {
        if (string.IsNullOrEmpty(s)) return "";
        int i;
        int len = s.Length;
        StringBuilder sb = new StringBuilder(len + 4);
        string t;
        for (i = 0; i < len; i += 1)
        {
            char c = s[i];
            switch (c)
            {
                case '>':
                case '"':
                case '\\':
                    sb.Append('\\');
                    sb.Append(c);
                    break;
                case '\b':
                    sb.Append("\\b");
                    break;
                case '\t':
                    sb.Append("\\t");
                    break;
                case '\n':
                    break;
                case '\f':
                    sb.Append("\\f");
                    break;
                case '\r':
                    break;
                default:
                    if (c < ' ')
                    {
                        string tmp = new string(c, 1);
                        t = "000" + int.Parse(tmp, System.Globalization.NumberStyles.HexNumber);
                        sb.Append("\\u" + t.Substring(t.Length - 4));
                    }
                    else
                    {
                        sb.Append(c);
                    }
                    break;
            }
        }
        return sb.ToString();
    }
}

任何帮助都非常感谢!谢谢!

Any help is greatly appreciated! Thanks!

推荐答案

经过几个小时的修补,我终于想出了我做错了什么!原来我真的应该阅读一些文档。 EntityState 有几个不同的属性,其中一些是修改添加已删除。通过表示这些状态之一,我可以直接从编辑方法插入和删除。

After a few hours of tinkering, I finally figured out what I was doing wrong! It turns out I really should read some documentation. EntityState has several different properties, some of which are Modified, Added, and Deleted. By denoting one of these states I can insert and delete directly from the edit method.

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(employees1 employees1)
    {
        if (ModelState.IsValid)
        {
            foreach (var item in employees1.employee_phone_manager)
            {
                if (item.deleted)
                {
                    db.Entry(item).State = EntityState.Deleted;
                }
                else if (item.phone_id == 0)
                {
                    db.Entry(item).State = EntityState.Added;
                }
                else
                {
                    db.Entry(item).State = EntityState.Modified;
                }

                db.SaveChanges();
            }

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

            return RedirectToAction("Index");
        }
        ViewBag.phonetype = new SelectList(db.phone_types, "phone_type_id", "phone_type_name");
        ViewBag.all_id = new SelectList(db.all_employees, "all_id", "all_id", employees1.all_id);
        return View(employees1);
    }

这篇关于在MVC中为嵌套集合添加更多项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆