【问题标题】:dictionary insert causes error 'an item with the same key has already been added'字典插入导致错误“已添加具有相同键的项目”
【发布时间】:2011-09-23 04:06:50
【问题描述】:

enter code here我一直在使用 ASP.NET MVC 3 应用程序遇到此异常。

该项目有两个简单的类。项目和订单。项目的创建工作正常,但是当我尝试创建订单时,它一直失败,并出现以下异常。

{"requestigAppLead":"ddd","re​​questingApplication":"sad","re​​sponsibleApp":"fff","re​​sponsibleAppLead":"ddd"}[ArgumentException: 已添加具有相同键的项目。 ] System.ThrowHelper.ThrowArgumentException(异常资源资源)+52 System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) +9374523 System.Linq.Enumerable.ToDictionary(IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer`1 comparer) +252

我仍然看到同样的问题。以下是完整的详细信息。

@model DART.Models.Order

@{
    ViewBag.Title = "Create";
}

<h2>Submit your order.</h2>

<script type="text/javascript">
    $(function () {

        $("#orderStatusID").change(function () {
            if ($("#orderStatusID").val() == "8") { // blocked by defect
                $("#divDefectDetails").show();
            }
            else {
                $("#divDefectDetails").hide();
            }
        });


        $("#projectID").change(function () {
            //alert("select changed!!!" + $("#projectID").val());
            var ur = "/SelectProject/" + $("#projectID").val();
            var query = $("#projectID").val();


            if (query == "") {
                $("#requesterLead").val("");
                $("#requestingApp").val("");
                $("#responsibleApp").val("");
                $("#responsibleLead").val("");
                return;
            }

            // get the JASON object back from Controler.
            // use the data to load the values into form
            $.getJSON("/Order/SelectProject", { id: query }, function (data) {
                //alert("data===" + data);
                jQuery.each(data, function (key, val) {
                    if (key == "requestigAppLead") {
                        $("#requesterLead").val(val);
                    }
                    else if (key == "requestingApplication") {
                        $("#requestingApp").val(val);
                    }
                    else if (key == "responsibleApp") {
                        $("#responsibleApp").val(val);
                    }
                    else if (key == "responsibleAppLead") {
                        $("#responsibleLead").val(val);
                    }

                });
            });
        });
    })
</script>


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Enter Your Order Detalis</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.projectProjectID, "Project")
        </div>
        <div class="editor-field" id="divProjectId">
            @Html.DropDownList("projectID","Select Project")
            @Html.ValidationMessageFor(model => model.projectProjectID)
        </div>
        <div class="editor-label">
            @Html.Label("Requester Lead")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.requesterLead)
            @Html.ValidationMessageFor(model => model.requesterLead)
        </div>

        <div class="editor-label">
            @Html.Label("Responsible CUID")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.responsibleCUID)
            @Html.ValidationMessageFor(model => model.responsibleCUID)
        </div>

        <div class="editor-label">
            @Html.Label("Responsible Lead")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.responsibleLead)
            @Html.ValidationMessageFor(model => model.responsibleLead)
        </div>

        <div class="editor-label">
            @Html.Label("Requesting Application")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.requestingApp)
            @Html.ValidationMessageFor(model => model.requestingApp)
        </div>

        <div class="editor-label">
            @Html.Label("Responsible Application")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.responsibleApplication)
            @Html.ValidationMessageFor(model => model.responsibleApplication)
        </div>

        <div class="editor-label">
            @Html.Label("RCBSCode")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.rcbsCode)
            @Html.ValidationMessageFor(model => model.rcbsCode)
        </div>

        <div class="editor-label">
            @Html.Label("Order Request Details")
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.orderRequestDetails,"width=100%")
            @Html.ValidationMessageFor(model => model.orderRequestDetails)
        </div>

        <div class="editor-label">
            @Html.Label("Order Request Complete Details")
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.orderRequestCompleteDetails)
            @Html.ValidationMessageFor(model => model.orderRequestCompleteDetails)
        </div>

        <div class="editor-label">
            @Html.Label("CreatedBy")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.createdby)
            @Html.ValidationMessageFor(model => model.createdby)
        </div>

        <div class="editor-label">
            @Html.Label("UpdatedBy")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.updatedBy)
            @Html.ValidationMessageFor(model => model.updatedBy)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.status, "OrderStatus")
        </div>
        <div class="editor-field" id="divStatus">
            @Html.DropDownList("orderStatusID", "Select Order Status")
            @Html.ValidationMessageFor(model => model.Status)
        </div>

        <div id="divDefectDetails" style="display:none">

            <div class="editor-label">
                @Html.Label("Defect ID")
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.defectID)
                @Html.ValidationMessageFor(model => model.defectID)
            </div>

            <div class="editor-label">
                @Html.Label("QC Project Name")
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.qcProjectName)
                @Html.ValidationMessageFor(model => model.qcProjectName)
            </div>

            <div class="editor-label">
                @Html.Label("QC Application")
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.application)
                @Html.ValidationMessageFor(model => model.application)
            </div>

            <div class="editor-label">
                @Html.Label("QC Status")
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.status)
                @Html.ValidationMessageFor(model => model.status)
            </div>

            <div class="editor-label">
                @Html.Label("QC EFD")
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.efd)
                @Html.ValidationMessageFor(model => model.efd)
            </div>

        </div>


        <p>
            <input type="submit" id="CreateOrder" name="CreateOrder" value="Create Order" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Control Code:

//
// GET: /Order/Create
public ActionResult Create( )
{
    ViewBag.projectID = new SelectList(db.Projects.ToList(), "projectID", "projectName");
    ViewBag.orderStatusID = new SelectList(db.OrderStatuses.ToList(), "orderStatusID", "description");
    return View();
}

按以下要求添加更多详细信息。

    //
    // Populate project details
    //
    [HttpGet]
    public virtual JsonResult SelectProject(long? id)
    {
        Project project = this.db.Projects.Find(1);
        var result = new { project.requestigAppLead,
                           project.requestingApplication,
                            project.responsibleApp,
                           project.responsibleAppLead
                          };


        //ViewBag.projectID = new SelectList(db.Projects, "projectID", "projectName",id.ToString());

        //return View(order);
        return Json(result, JsonRequestBehavior.AllowGet);
    }

            //
    // POST: /Order/Create
    [HttpPost, ActionName("Create")]
    public ActionResult Create(Order order)
    {
        if (ModelState.IsValid)
        {
            //
            // TO DO: if the order count is more than threshold raise an alert to the user and send an email to 
            // leads
            //

            order.createDateTime = DateTime.Now;
            order.updateDateTime = DateTime.Now;
            db.Orders.Add(order);
            db.SaveChanges();

            // send email
            util.EmailUtil.SendEmail(order);

            return RedirectToAction("Index");
        }

        ViewBag.projectID = new SelectList(db.Projects, "projectID", "projectName", order.projectProjectID);
        return View(order);
    }

【问题讨论】:

  • 异常不如实际导致异常的代码重要。编辑您的问题以包含您要插入字典的代码。
  • JSON 是否显示了失败的请求的整个正文?如果没有,请显示所有提交的 JSON。
  • 我添加了上面的代码来显示 HttpGet 和 HttpPost 方法。还包括一个方法,当用户选择 Project Dropdown 时用于获取 Jason 数据。

标签: asp.net-mvc asp.net-mvc-3


【解决方案1】:

您的 linq 查询包含重复条目。您需要弄清楚那是什么并将其删除,或者如果可能的话使用 .ToList() 而不是 ToDictionary()

【讨论】:

    猜你喜欢
    • 2013-03-18
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    相关资源
    最近更新 更多