2011-09-22 38 views
0

enter code here我一直使用ASP.NET MVC 3應用程序運行此異常。字典插入導致錯誤'具有相同密鑰的項目已被添加'

該項目有兩個簡單的分類。項目和訂單。項目的創建工作正常,但是當我嘗試創建一個訂單時,它仍然與下面的說明一起失敗。

{ 「requestigAppLead」: 「DDD」, 「requestingApplication」: 「悲」, 「responsibleApp」: 「FFF」, 「responsibleAppLead」: 「DDD」} [ArgumentException的:具有相同鍵的項已被添加。] System.ThrowHelper.ThrowArgumentException(ExceptionResource資源)52 System.Collections.Generic.Dictionary 2.Insert(TKey key, TValue value, Boolean add) +9374523 System.Linq.Enumerable.ToDictionary(IEnumerable 1種源,函數功能2 keySelector, Func 2 elementSelector,IEqualityComparer`1比較器)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); 
    } 
+0

這個異常不像實際導致異常**的**代碼那麼重要。編輯您的問題以將代碼添加到字典中。 –

+0

JSON是否顯示請求失敗的整個機構?如果沒有,請顯示所有正在提交的JSON。 – counsellorben

+0

我已經添加了上面的代碼來顯示HttpGet和HttpPost方法。當用戶選擇項目下拉菜單時,也包括一種用於獲取Jason數據的方法。 – user959671

回答

0

您的linq查詢包含重複條目。你需要弄清楚它是什麼並刪除它或者如果可能的話使用.ToList()而不是ToDictionary()

相關問題