2012-01-27 112 views
2

我已經啓動了此「支持票證」項目,用戶可以在其中創建票證,並且管理員和用戶都可以作出迴應。我是MVC3的新手。我自己解決了大多數問題,但是我花了大約5個小時來搜索Google/Stackoverflow,我無法弄清楚......所以我來找專家。INSERT語句與FOREIGN KEY衝突

這是錯誤:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Reponses_Tickets1". The conflict occurred in database "C:\USERS\ME\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\TICKETSYSTEM\TICKETSYSTEM\APP_DATA\TICKETSYSTEM.MDF", table "dbo.Tickets", column 'ID'.

源錯誤:

Line 21:    response.Body = body; 
Line 22:    model.AddToReponses(response); 
Line 23:    model.SaveChanges(); 
Line 24:    return RedirectToAction("Ticket", new { id = id }); 
Line 25:   } 

以下是完整的控制器動作:

 public ActionResult Responses(int id, string body) 
     { 
      Ticket ticket = GetTicket(id); 
      Response response = new Response(); 
      response.Body = body; 
      model.AddToReponses(response); 
      model.SaveChanges(); 
      return RedirectToAction("Ticket", new { id = id }); 
     } 

對於那些想知道,我一直按照數學的教程「從零到100分鐘的博客」 ewblagden(YouTube),但用它作爲門票系統而不是博客。我可以告訴他,他一行一行地跟着他。

任何幫助,將不勝感激。謝謝。

+0

你用什麼來訪問數據? ActiveRecord,實體框架,LINQ to SQL等... – jrummell 2012-01-27 21:54:22

回答

3

假設您的每個響應都以某種方式與票證關聯,您還需要在代碼中執行此操作!

public ActionResult Responses(int id, string body) 
{ 
    // get the ticket 
    Ticket ticket = GetTicket(id); 

    // create resposne 
    Response response = new Response(); 
    response.Body = body; 

    // *** BEGIN NEW SECTION *** 
    // here, you need to ASSOCIATE your response to the ticket you're retrieved! 
    response.Ticket = ticket; // or something like that...... 

    // or maybe: 
    response.TicketId = ticket.Id; // or something like that...... 
    // *** END NEW SECTION *** 

    model.AddToReponses(response); 
    model.SaveChanges(); 

    return RedirectToAction("Ticket", new { id = id }); 
} 

現在,你有單機反應(不與任何Ticket相關) - 這顯然並不在你的數據庫。

+0

工作!非常感謝你! – Clayton 2012-01-27 22:13:44

相關問題